new_test.go 4.4 KB
Newer Older
1
2
3
package tests

import (
4
	"fmt"
5
6
7
8
9
10
	"io/ioutil"
	"path/filepath"
	"strings"
	"testing"
	"time"

11
	"github.com/stretchr/testify/assert"
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
)

// 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)
33
	_, _, _, stop, ca, err := startPlatform(workingDir)
34
	assert.Equal(t, nil, err)
35
	defer stop()
36
37
38
39

	time.Sleep(2 * time.Second)

	// Register client1
Loïck Bonniot's avatar
Loïck Bonniot committed
40
	client1, err := createClient(workingDir, ca, 0)
41
	assert.Equal(t, nil, err)
ElyKar's avatar
ElyKar committed
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	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)
64
	assert.Equal(t, "6a95f6bcd6282186a7b1175fbaab4809ca5f665f7c4d55675de2399c83e67252069d741a88c766b1a79206d6dfbd5552cd7f9bc69b43bee161d1337228b4a4a8", fmt.Sprintf("%x", contract.File.Hash))
65
66
67
	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)
68
69
	assert.True(t, len(contract.Signers[0].Hash) > 0)
	assert.True(t, len(contract.Signers[1].Hash) == 0)
70
71

	// Register second signer
Loïck Bonniot's avatar
Loïck Bonniot committed
72
	client2, err := createClient(workingDir, ca, 0)
73
74
75
76
77
	assert.Equal(t, nil, err)
	err = registerAndAuth(client2, "client2@example.com", "password2", "", true, true)
	assert.Equal(t, nil, err)

	// Check database²
78
	time.Sleep(time.Second) // Allowed delay to let some time to propagate the contract readiness
79
80
	contract = getContract("contract.txt", 0)
	assert.Equal(t, true, contract.Ready)
81
82
	assert.True(t, len(contract.Signers[0].Hash) > 0)
	assert.True(t, len(contract.Signers[1].Hash) > 0)
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

	// 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)
102
103
	assert.True(t, len(contract.Signers[0].Hash) > 0)
	assert.True(t, len(contract.Signers[1].Hash) > 0)
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

	// 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
Loïck Bonniot's avatar
Loïck Bonniot committed
120
	client3, err := createClient(workingDir, ca, 0)
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
	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())
}