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 pathprofile_test.go
More file actions
138 lines (108 loc) · 3.76 KB
/
profile_test.go
File metadata and controls
138 lines (108 loc) · 3.76 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
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/dhable/mini-auth/app"
"github.com/dhable/mini-auth/models"
)
func TestProfile(t *testing.T) {
app, err := app.NewApp()
if err != nil {
t.Fatalf("failed to create application instance: %s", err)
}
t.Run("missing header", func(t *testing.T) {
req, err := http.NewRequest("GET", "/profile", nil)
if err != nil {
t.Fatalf("failed to create http request: %s", err)
}
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(app.CurrentUserProfile)
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("invalid authorization value", func(t *testing.T) {
req, err := http.NewRequest("GET", "/profile", nil)
if err != nil {
t.Fatalf("failed to create http request: %s", err)
}
req.Header.Add("Authorization", "garbage values")
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(app.CurrentUserProfile)
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("unknown jwt", func(t *testing.T) {
req, err := http.NewRequest("GET", "/profile", nil)
if err != nil {
t.Fatalf("failed to create http request: %s", err)
}
jwt := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c`
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", jwt))
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(app.CurrentUserProfile)
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("valid user jwt", func(t *testing.T) {
// Obtains a JWT using the authenticate endpoint
body := `{
"email": "dan@danhable.com",
"password": "password1!"
}`
req, err := http.NewRequest("POST", "/authenicate", strings.NewReader(body))
if err != nil {
t.Fatalf("failed to create authenicate http request: %s", err)
}
recorder := httptest.NewRecorder()
handler := http.HandlerFunc(app.AuthenticateUser)
handler.ServeHTTP(recorder, req)
var authResp models.AuthResponse
err = json.Unmarshal(recorder.Body.Bytes(), &authResp)
if err != nil {
t.Errorf("failed to parse body: %s", err)
}
// Gets profile with that JWT
req, err = http.NewRequest("GET", "/profile", nil)
if err != nil {
t.Fatalf("failed to create profile http request: %s", err)
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", authResp.Jwt))
recorder = httptest.NewRecorder()
handler = http.HandlerFunc(app.CurrentUserProfile)
handler.ServeHTTP(recorder, req)
status := recorder.Code
if status != http.StatusOK {
t.Errorf("incorrect status code: got %v want %v", status, http.StatusOK)
}
var profileResp models.UserProfile
err = json.Unmarshal(recorder.Body.Bytes(), &profileResp)
if err != nil {
t.Errorf("failed to parse profile body: %s", err)
}
expectedEmail := "dan@danhable.com"
if profileResp.Email != expectedEmail {
t.Errorf("incorrect profile email: got %s want %s", profileResp.Email, expectedEmail)
}
expectedName := "Dan Hable"
if profileResp.Name != expectedName {
t.Errorf("incorrect profile name: got %s want %s", profileResp.Name, expectedName)
}
expectedLocation := "Chicago, IL"
if profileResp.Location != expectedLocation {
t.Errorf("incorrect profile location: got %s want %s", profileResp.Location, expectedLocation)
}
})
}