This repository was archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticate_test.go
More file actions
110 lines (89 loc) · 2.69 KB
/
authenticate_test.go
File metadata and controls
110 lines (89 loc) · 2.69 KB
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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/dhable/mini-auth/app"
"github.com/dhable/mini-auth/models"
)
func TestAuthenticate(t *testing.T) {
app, err := app.NewApp()
if err != nil {
t.Fatalf("failed to create application instance: %s", err)
}
t.Run("bad request", func(t *testing.T) {
body := `{"something": "invalid"}`
req, err := http.NewRequest("POST", "/authenicate", strings.NewReader(body))
if err != nil {
t.Fatalf("failed to create http request: %s", err)
}
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(app.AuthenticateUser)
handler.ServeHTTP(recorder, req)
status := recorder.Code
if status != http.StatusBadRequest {
t.Errorf("incorrect status code: got %v want %v", status, http.StatusBadRequest)
}
})
t.Run("user doesn't exist", func(t *testing.T) {
body := `{
"email": "nobody@danhable.com",
"password": "password1!"
}`
req, err := http.NewRequest("POST", "/authenicate", strings.NewReader(body))
if err != nil {
t.Fatalf("failed to create http request: %s", err)
}
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(app.AuthenticateUser)
handler.ServeHTTP(recorder, req)
status := recorder.Code
if status != http.StatusUnauthorized {
t.Errorf("incorrect status code: got %v want %v", status, http.StatusUnauthorized)
}
})
t.Run("user has incorrect password", func(t *testing.T) {
body := `{
"email": "dan@danhable.com",
"password": "ssssh"
}`
req, err := http.NewRequest("POST", "/authenicate", strings.NewReader(body))
if err != nil {
t.Fatalf("failed to create http request: %s", err)
}
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(app.AuthenticateUser)
handler.ServeHTTP(recorder, req)
status := recorder.Code
if status != http.StatusUnauthorized {
t.Errorf("incorrect status code: got %v want %v", status, http.StatusUnauthorized)
}
})
t.Run("correct credentials", func(t *testing.T) {
body := `{
"email": "dan@danhable.com",
"password": "password1!"
}`
req, err := http.NewRequest("POST", "/authenicate", strings.NewReader(body))
if err != nil {
t.Fatalf("failed to create http request: %s", err)
}
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(app.AuthenticateUser)
handler.ServeHTTP(recorder, req)
status := recorder.Code
if status != http.StatusOK {
t.Errorf("incorrect status code: got %v want %v", status, http.StatusOK)
}
var resp models.AuthResponse
err = json.Unmarshal(recorder.Body.Bytes(), &resp)
if err != nil {
t.Errorf("failed to parse body: %s", err)
}
if resp.Jwt == "" {
t.Error("expected Jwt value")
}
})
}