-
Notifications
You must be signed in to change notification settings - Fork 3
/
model_test.go
66 lines (52 loc) · 1.57 KB
/
model_test.go
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
package main
import "testing"
func TestCreateAccount(t *testing.T) {
account, err := NewAccount("sample", "secret")
if err != nil {
t.Fatalf("Unable to create account: %v", err)
}
if account.Name != "sample" {
t.Errorf("Account had unexpected name: %s", account.Name)
}
if account.Administrator {
t.Error("Account unexpectedly had administrator rights.")
}
if account.CreatedAt == 0 {
t.Error("Account did not have creation time populated")
}
if account.CreatedAt != account.UpdatedAt {
t.Errorf("Account creation and update time differ: %d == %d",
account.CreatedAt, account.UpdatedAt)
}
}
func TestHasPassword(t *testing.T) {
account, err := NewAccount("sample", "secret")
if err != nil {
t.Fatalf("Unable to create account: %v", err)
}
if !account.HasPassword("secret") {
t.Error("Correct password not accepted")
}
if account.HasPassword("wrong") {
t.Error("Incorrect password accepted")
}
}
func TestGenerateAPIKey(t *testing.T) {
account, err := NewAccount("sample", "secret")
if err != nil {
t.Fatalf("Unable to create account: %v", err)
}
if len(account.APIKeys) != 1 {
t.Errorf("Expected newly created account to have 1 API key, but had %d", len(account.APIKeys))
}
key, err := account.GenerateAPIKey()
if err != nil {
t.Errorf("Unexpected error generating an API key: %v", err)
}
if len(account.APIKeys) != 2 {
t.Errorf("Expected account to have two API keys, but had %d", len(account.APIKeys))
}
if account.APIKeys[1] != key {
t.Errorf("Expected the generated key [%s] to match the account [%s]", key, account.APIKeys[1])
}
}