contract_test.go 4.41 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
package contract_test // Using another package to avoid import cycles

import (
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"dfss/dfssp/entities"
	"dfss/dfssp/server"
	"dfss/mgdb"
	"dfss/net"
	"github.com/bmizerany/assert"
	"gopkg.in/mgo.v2/bson"
)

var err error
var collection *mgdb.MongoCollection
var manager *mgdb.MongoManager
var dbURI string

var repository *entities.ContractRepository

func TestMain(m *testing.M) {

	dbURI = os.Getenv("DFSS_MONGO_URI")
	if dbURI == "" {
		dbURI = "mongodb://localhost/dfss-test"
	}

	manager, err = mgdb.NewManager(dbURI)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

37
	collection = manager.Get("contracts")
38 39 40 41 42
	repository = entities.NewContractRepository(collection)

	// Start platform server
	keyPath := filepath.Join(os.Getenv("GOPATH"), "src", "dfss", "dfssp", "testdata")

Loïck Bonniot's avatar
Loïck Bonniot committed
43
	srv := server.GetServer(keyPath, dbURI, 365, true)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	go func() { _ = net.Listen("localhost:9090", srv) }()

	// Run
	code := m.Run()

	dropDataset()
	manager.Close()
	os.Exit(code)
}

func TestAddSigner(t *testing.T) {
	c := entities.NewContract()

	id := bson.NewObjectId()

59 60
	c.AddSigner(nil, "mail1", []byte{0xaa})
	c.AddSigner(&id, "mail2", []byte{})
61 62 63 64 65 66 67 68

	signers := c.Signers

	if len(signers) != 2 {
		t.Fatal("Signers are not inserted correctly")
	}

	assert.Equal(t, signers[0].Email, "mail1")
69
	assert.Equal(t, signers[0].Hash, []byte{0xaa})
70 71
	assert.Equal(t, signers[0].UserID.Hex(), "000000000000000000000000")
	assert.Equal(t, signers[1].Email, "mail2")
72
	assert.Equal(t, signers[1].Hash, []byte{})
73 74 75
	assert.Equal(t, signers[1].UserID.Hex(), id.Hex())
}

Loïck Bonniot's avatar
Loïck Bonniot committed
76 77 78 79 80 81 82 83 84 85 86 87 88
func TestGetHashChain(t *testing.T) {
	c := entities.NewContract()
	c.AddSigner(nil, "mail1", []byte{0xaa})
	c.AddSigner(nil, "mail2", []byte{0xbb, 0xcc})
	c.AddSigner(nil, "mail3", []byte{})

	chain := c.GetHashChain()
	assert.Equal(t, 3, len(chain))
	assert.Equal(t, []byte{0xaa}, chain[0])
	assert.Equal(t, []byte{0xbb, 0xcc}, chain[1])
	assert.Equal(t, []byte{}, chain[2])
}

89 90 91 92 93 94 95 96 97 98
func assertContractEqual(t *testing.T, contract, fetched entities.Contract) {
	assert.Equal(t, contract.File, fetched.File)
	assert.Equal(t, contract.Date.Unix(), fetched.Date.Unix())
	assert.Equal(t, contract.Comment, fetched.Comment)
	assert.Equal(t, contract.Ready, fetched.Ready)
	assert.Equal(t, contract.Signers, fetched.Signers)
}

// Insert a contract with 2 users and check the fields are correctly persisted
func TestInsertContract(t *testing.T) {
99
	dropDataset()
100
	c := entities.NewContract()
101 102
	c.AddSigner(nil, "mail1", []byte{0xaa})
	c.AddSigner(nil, "mail1", []byte{0xaa})
103
	c.File.Name = "file"
104
	c.File.Hash = []byte{0xff}
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	c.File.Hosted = false
	c.Comment = "comment"
	c.Ready = true

	_, err := repository.Collection.Insert(c)
	if err != nil {
		t.Fatal("Contract not inserted successfully in the database:", err)
	}

	fetched := entities.Contract{}
	selector := entities.Contract{
		ID: c.ID,
	}
	err = repository.Collection.FindByID(selector, &fetched)

	if err != nil {
		t.Fatal("Contract could not be successfully retrieved:", err)
	}

	assertContractEqual(t, *c, fetched)
}
126 127 128 129 130

// Insert some contracts with missing user and test waiting contracts for this user
func TestGetWaitingForUser(t *testing.T) {
	knownID := bson.NewObjectId()
	dropDataset()
Loïck Bonniot's avatar
Loïck Bonniot committed
131

132
	c1 := entities.NewContract()
133
	c1.AddSigner(nil, "mail1", []byte{})
134 135 136
	c1.Ready = false

	c2 := entities.NewContract()
137 138
	c2.AddSigner(nil, "mail1", []byte{})
	c2.AddSigner(&knownID, "mail2", []byte{0x12})
139 140 141
	c2.Ready = false

	c3 := entities.NewContract()
142 143
	c3.AddSigner(nil, "mail2", []byte{})
	c3.AddSigner(&knownID, "mail1", []byte{0xaa})
144 145 146 147 148 149 150 151 152 153
	c3.Ready = false

	_, _ = repository.Collection.Insert(c1)
	_, _ = repository.Collection.Insert(c2)
	_, _ = repository.Collection.Insert(c3)

	contracts, err := repository.GetWaitingForUser("mail1")
	assert.Equal(t, nil, err)
	assert.Equal(t, 2, len(contracts))
}
Loïck Bonniot's avatar
Loïck Bonniot committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

func TestCheckAuthorization(t *testing.T) {
	dropDataset()
	createDataset()
	id := addTestContract()

	assert.T(t, repository.CheckAuthorization(user1.CertHash, id))
	assert.T(t, !repository.CheckAuthorization(user1.CertHash, bson.NewObjectId()))
	assert.T(t, !repository.CheckAuthorization(user2.CertHash, id))
	assert.T(t, !repository.CheckAuthorization(user2.CertHash, bson.NewObjectId()))

	contract := entities.Contract{}
	_ = repository.Collection.FindByID(entities.Contract{ID: id}, &contract)
	contract.Ready = false
	_, _ = repository.Collection.UpdateByID(contract)

	// Not valid if contract is not ready
	assert.T(t, !repository.CheckAuthorization(user1.CertHash, id))
}