join_test.go 1.91 KB
Newer Older
Loïck Bonniot's avatar
Loïck Bonniot committed
1
2
3
4
5
6
7
package contract_test

import (
	"testing"

	"dfss/dfssp/api"
	"dfss/dfssp/entities"
8
	"github.com/stretchr/testify/assert"
Loïck Bonniot's avatar
Loïck Bonniot committed
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
	"golang.org/x/net/context"
	"gopkg.in/mgo.v2/bson"
)

func addTestContract() bson.ObjectId {
	contract := entities.NewContract()
	contract.AddSigner(&user1.ID, user1.Email, user1.CertHash)
	contract.Ready = true
	_, _ = manager.Get("contracts").Insert(contract)
	return contract.ID
}

func TestJoinSignature(t *testing.T) {
	dropDataset()
	createDataset()
	contractID := addTestContract()

	client := clientTest(t)
	stream, err := client.JoinSignature(context.Background(), &api.JoinSignatureRequest{
		ContractUuid: contractID.Hex(),
		Port:         5050,
	})
	assert.Equal(t, nil, err)

	user, err := stream.Recv()
	assert.Equal(t, nil, err)
	assert.Equal(t, "", user.ErrorCode.Message)
	assert.Equal(t, api.ErrorCode_SUCCESS, user.ErrorCode.Code)
	assert.Equal(t, contractID.Hex(), user.ContractUuid)
	assert.Equal(t, "test@test.com", user.User.Email)
	assert.Equal(t, uint32(5050), user.User.Port)
}

func TestJoinSignatureBadContract(t *testing.T) {
	dropDataset()
	createDataset()

	client := clientTest(t)
	stream, err := client.JoinSignature(context.Background(), &api.JoinSignatureRequest{
		ContractUuid: bson.NewObjectId().Hex(),
		Port:         5050,
	})
	assert.Equal(t, nil, err)

	user, err := stream.Recv()
	assert.Equal(t, nil, err)
	assert.Equal(t, "unauthorized signature", user.ErrorCode.Message)
	assert.Equal(t, api.ErrorCode_INVARG, user.ErrorCode.Code)
}
Loïck Bonniot's avatar
Loïck Bonniot committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

func TestJoinSignatureBadUUID(t *testing.T) {
	dropDataset()
	createDataset()

	client := clientTest(t)
	stream, err := client.JoinSignature(context.Background(), &api.JoinSignatureRequest{
		ContractUuid: "VERY_BAD",
		Port:         5050,
	})
	assert.Equal(t, nil, err)

	user, err := stream.Recv()
	assert.Equal(t, nil, err)
	assert.Equal(t, "invalid contract uuid", user.ErrorCode.Message)
	assert.Equal(t, api.ErrorCode_INVARG, user.ErrorCode.Code)
}