new_test.go 4.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
package tests

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/bmizerany/assert"
)

// TestNewContract tries to creates new contracts on the platform.
//
// CLASSIC SCENARIO
// 1. client1 registers on the platform
// 2. client1 sends a new contract on the platform, but client2 is not here yet
// 3. client2 registers on the platform
// 4. client2 sends a new contract on the platform, and everyone is here
//
// BAD CASES
// 1. client2 sends a new contract with a wrong password
// 2. client3 sends a new contract without authentication
// 3. client1 sends a new contract with an invalid filepath
func TestNewContract(t *testing.T) {
	// Cleanup
	eraseDatabase()

	// Start the platform
	workingDir, err := ioutil.TempDir("", "dfss_")
	assert.Equal(t, nil, err)
	platform, ca, err := startPlatform(workingDir)
	assert.Equal(t, nil, err)
	defer func() {
		_ = platform.Process.Kill()
		_ = os.RemoveAll(workingDir)
	}()

	time.Sleep(2 * time.Second)

	// Register client1
	client1, err := createClient(workingDir, ca)
	assert.Equal(t, nil, err)
	err = registerAndAuth(client1, "client1@example.com", "password", "", true, true)
	assert.Equal(t, nil, err)

	// Create contract
	client1 = newClient(client1)
	setLastArg(client1, "new", true)
	client1.Stdin = strings.NewReader(
		"password\n" +
			filepath.Join("testdata", "contract.txt") + "\n" +
			"A very nice comment\n" +
			"client1@example.com\n" +
			"client2@example.com\n" +
			"\n",
	)
	err = checkStderr(t, client1, "Operation succeeded with a warning message: Some users are not ready yet\n")
	assert.NotEqual(t, nil, err)

	// Check database
	contract := getContract("contract.txt", 0)
	assert.Equal(t, false, contract.Ready)
	assert.Equal(t, "A very nice comment", contract.Comment)
	assert.Equal(t, "6a95f6bcd6282186a7b1175fbaab4809ca5f665f7c4d55675de2399c83e67252069d741a88c766b1a79206d6dfbd5552cd7f9bc69b43bee161d1337228b4a4a8", contract.File.Hash)
	assert.Equal(t, 2, len(contract.Signers))
	assert.Equal(t, "client1@example.com", contract.Signers[0].Email)
	assert.Equal(t, "client2@example.com", contract.Signers[1].Email)
	assert.T(t, len(contract.Signers[0].Hash) > 0)
	assert.T(t, len(contract.Signers[1].Hash) == 0)

	// Register second signer
	client2, err := createClient(workingDir, ca)
	assert.Equal(t, nil, err)
	err = registerAndAuth(client2, "client2@example.com", "password2", "", true, true)
	assert.Equal(t, nil, err)

	// Check database²
	contract = getContract("contract.txt", 0)
	assert.Equal(t, true, contract.Ready)
	assert.T(t, len(contract.Signers[0].Hash) > 0)
	assert.T(t, len(contract.Signers[1].Hash) > 0)

	// Create a second contract
	client2 = newClient(client2)
	setLastArg(client2, "new", true)
	client2.Stdin = strings.NewReader(
		"password2\n" +
			filepath.Join("testdata", "contract.txt") + "\n" +
			"Another comment with some accents héhé\n" +
			"client1@example.com\n" +
			"client2@example.com\n" +
			"\n",
	)
	err = checkStderr(t, client2, "")
	assert.Equal(t, nil, err)

	// Check database³
	contract = getContract("contract.txt", 1)
	assert.Equal(t, true, contract.Ready)
	assert.Equal(t, "Another comment with some accents héhé", contract.Comment)
	assert.T(t, len(contract.Signers[0].Hash) > 0)
	assert.T(t, len(contract.Signers[1].Hash) > 0)

	// Bad case: wrong password
	client2 = newClient(client2)
	setLastArg(client2, "new", true)
	client2.Stdin = strings.NewReader(
		"wrongPwd\n" +
			filepath.Join("testdata", "contract.txt") + "\n" +
			"\n" +
			"client1@example.com\n" +
			"client2@example.com\n" +
			"\n",
	)
	err = checkStderr(t, client2, "x509: decryption password incorrect\n")
	assert.NotEqual(t, nil, err)

	// Bad case: no authentication
	client3, err := createClient(workingDir, ca)
	setLastArg(client3, "new", false)
	client3.Stdin = strings.NewReader(
		"\n" +
			filepath.Join("testdata", "contract.txt") + "\n" +
			"\n" +
			"client1@example.com\n" +
			"\n",
	)
	err = client3.Run()
	assert.NotEqual(t, nil, err)

	// Bad case: bad filepath
	client1 = newClient(client1)
	setLastArg(client1, "new", true)
	client1.Stdin = strings.NewReader(
		"password\n" +
			"invalidFile\n" +
			"client1@example.com\n" +
			"\n",
	)
	err = checkStderr(t, client1, "open invalidFile: no such file or directory\n")
	assert.NotEqual(t, nil, err)

	// Check number of stored contracts
	assert.Equal(t, 2, dbManager.Get("contracts").Count())
}