-
Notifications
You must be signed in to change notification settings - Fork 2
/
users_test.go
113 lines (95 loc) · 2.56 KB
/
users_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//nolint
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestUsersInit(t *testing.T) {
expected := "test"
UsersInit(expected)
actual := UsersURI
if actual != expected {
t.Errorf("URI was %s, not %s", actual, expected)
}
}
func TestNewUser(t *testing.T) {
expectedURI := "test-URI"
expectedID := "test-user"
UsersInit(expectedURI)
u := NewUser(expectedID)
if u.URI != expectedURI {
t.Errorf("URI was %s, not %s", u.URI, expectedURI)
}
if u.ID != expectedID {
t.Errorf("id was %s, not %s", u.ID, expectedID)
}
}
func TestGet(t *testing.T) {
expectedURI := "URI"
UsersInit(expectedURI)
expected := NewUser("id")
expected.Name = "first-name last-name"
expected.FirstName = "first-name"
expected.LastName = "last-name"
expected.Email = "[email protected]"
expected.Institution = "institution"
expected.SourceID = "source-id"
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
actualPath := r.URL.Path
expectedPath := fmt.Sprintf("/subjects/%s", expected.ID)
if actualPath != expectedPath {
t.Errorf("path was %s, not %s", actualPath, expectedPath)
}
msg, err := json.Marshal(expected)
if err != nil {
t.Error(err)
}
w.Write(msg)
}))
defer srv.Close()
actual := NewUser("id")
actual.URI = srv.URL
err := actual.Get(context.Background())
if err != nil {
t.Error(err)
}
if actual.ID != expected.ID {
t.Errorf("id was %s, not %s", actual.ID, expected.ID)
}
if actual.Name != expected.Name {
t.Errorf("name was %s, not %s", actual.Name, expected.Name)
}
if actual.FirstName != expected.FirstName {
t.Errorf("first name was %s, not %s", actual.FirstName, expected.FirstName)
}
if actual.LastName != expected.LastName {
t.Errorf("last name was %s, not %s", actual.LastName, expected.LastName)
}
if actual.Email != expected.Email {
t.Errorf("email was %s, not %s", actual.Email, expected.Email)
}
if actual.Institution != expected.Institution {
t.Errorf("institution was %s, not %s", actual.Institution, expected.Institution)
}
if actual.SourceID != expected.SourceID {
t.Errorf("source ID was %s, not %s", actual.SourceID, expected.SourceID)
}
}
func TestParseID(t *testing.T) {
tests := map[string]string{
"test-user": "test-user",
"[email protected]": "test-user",
"test@[email protected]": "test@user",
"test@user@[email protected]": "test@user@one",
}
for k, expected := range tests {
actual := ParseID(k)
if actual != expected {
t.Errorf("id was %s, not %s", actual, expected)
}
}
}