-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration_endpoint_test.go
153 lines (113 loc) · 5.1 KB
/
registration_endpoint_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"context"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// https://github.com/tryvium-travels/memongo
// https://medium.com/@victor.neuret/mocking-the-official-mongo-golang-driver-5aad5b226a78
// Good repo with examples on how to mock mongoDB
// Here we are testing the API endpoints as a form of integration testing. This
// requires other dependent services to be available such as the database and
// cache so docker compose is used to facilitate orchestration
// Test case for successful user registration
func TestSuccessfulUserRegistration(t *testing.T) {
recorder := httptest.NewRecorder()
// Generate a new unique user email
newUserEmail := genRandomEmail()
// Register new user client JSON
user := `{"email": "` + newUserEmail + `", "password": "somePassword",
"firstName": "John", "lastName": "Smith", "groups": ["admin"]}`
// Create a new request
req, _ := http.NewRequest("POST", "/register-user", strings.NewReader(user))
// Send request to service
handler.ServeHTTP(recorder, req)
// Check if user is, indeed, inserted into the database
filter := bson.D{{Key: "email", Value: newUserEmail}}
mongoErr := clients.FindOne(context.Background(), filter).Err()
assert.NotErrorIs(t, mongo.ErrNoDocuments, mongoErr)
// Check for another database error
assert.Nil(t, mongoErr)
// Check if user creation response is sent to the client
assert.Equal(t, http.StatusCreated, recorder.Code)
// assert that the response body is what we expect
assert.Equal(t, `{"message":"User registered successfully"}`, recorder.Body.String())
}
func TestSuccessfulServiceRegistration(t *testing.T) {
recorder := httptest.NewRecorder()
// Generate a new unique service account email
// get current timestamp
time := time.Now().Unix()
// Create a new service account email
newServiceEmail := strconv.FormatInt(time, 10) + "@service.com"
// Register service client JSON
servicePayload := `{"email": "` + newServiceEmail + `", "name": "Service A",
"groups": ["tempProbes"]}`
// Create a new request
req, _ := http.NewRequest("POST", "/register-service", strings.NewReader(servicePayload))
// Send request to service
handler.ServeHTTP(recorder, req)
// Check if user is indeed inserted into the database
filter := bson.D{{Key: "email", Value: newServiceEmail}}
mongoErr := clients.FindOne(context.Background(), filter).Err()
assert.NotErrorIs(t, mongo.ErrNoDocuments, mongoErr)
// Check for another database error
assert.Nil(t, mongoErr)
// Check if user creation response is sent to the client
assert.Equal(t, http.StatusCreated, recorder.Code)
// Test that the body returns both the service API token and a success message
assert.Contains(t, recorder.Body.String(), `"apiToken"`)
}
// Test case for attempting to register a user that already exists
func TestFailedPreExistingUserRegistration(t *testing.T) {
recorder := httptest.NewRecorder()
// Generate a new unique user email
newUserEmail := genRandomEmail()
// Pre-insert a user into the database (so that we can attempt to register it again)
clients.InsertOne(context.Background(), bson.D{
{Key: "email", Value: newUserEmail},
{Key: "password", Value: "somePasswordHash"},
{Key: "firstName", Value: "John"},
{Key: "lastName", Value: "Smith"},
})
// New user json with same email as the pre-inserted user
user := `{"email": "` + newUserEmail + `", "password": "somePassword",
"firstName": "John", "lastName": "Smith", "groups": ["admin"]}`
// Create a new request
req, _ := http.NewRequest("POST", "/register-user", strings.NewReader(user))
// Send request to service
handler.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusConflict, recorder.Code)
assert.Equal(t, `{"message":"A user associated with the email address is already registered"}`, recorder.Body.String())
}
// Test case for attempting to register a user with an invalid payload
func TestFailedMissingFirstNameRegistration(t *testing.T) {
recorder := httptest.NewRecorder()
// New user json with missing firstName field
user := `{"email": "[email protected]", "password": "somePassword",
"lastName": "Smith", "groups": ["admin"]}`
// Create a new request
req, _ := http.NewRequest("POST", "/register-user", strings.NewReader(user))
// Send request to service
handler.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusBadRequest, recorder.Code)
assert.Equal(t, `{"message":"Key: 'UserRegistrationForm.FirstName' Error:Field validation for 'FirstName' failed on the 'required' tag"}`, recorder.Body.String())
}
func TestFailedInvalidJson(t *testing.T) {
recorder := httptest.NewRecorder()
user := `"email": "[email protected]",, "password": "somePassword",
"firstName": "John", "lastName": "Smith", "groups": ["admin"]}`
// Create a new request
req, _ := http.NewRequest("POST", "/register-user", strings.NewReader(user))
// Send request to service
handler.ServeHTTP(recorder, req)
assert.Equal(t, http.StatusBadRequest, recorder.Code)
assert.Equal(t, `{"message":"Invalid JSON payload"}`, recorder.Body.String())
}