starter_test.go 1.5 KB
Newer Older
Loïck Bonniot's avatar
Loïck Bonniot committed
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
package contract_test

import (
	"testing"

	"dfss/dfssp/api"
	"dfss/dfssp/entities"
	"github.com/bmizerany/assert"
	"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, "127.0.0.1", user.User.Ip)
	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)
}