-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
85 lines (74 loc) · 2.76 KB
/
Copy pathmain_test.go
File metadata and controls
85 lines (74 loc) · 2.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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
jwt "github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
)
func TestLoginHandlerReturnsSignedUserClaims(t *testing.T) {
originalSecret := jwtSecret
jwtSecret = "unit-test-secret"
t.Cleanup(func() { jwtSecret = originalSecret })
service := UserService{
Client: httpDoerFunc(func(req *http.Request) (*http.Response, error) {
return jsonResponse(http.StatusOK, `{"username":"admin","firstname":"Foo","lastname":"Bar","role":"admin"}`), nil
}),
UserAPIAddress: "http://users-api",
AllowedUserHashes: map[string]interface{}{"admin_admin": nil},
}
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader(`{"username":"admin","password":"admin"}`))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
recorder := httptest.NewRecorder()
if err := getLoginHandler(service)(e.NewContext(req, recorder)); err != nil {
t.Fatalf("login handler returned an error: %v", err)
}
if recorder.Code != http.StatusOK {
t.Fatalf("login status = %d, want %d", recorder.Code, http.StatusOK)
}
var payload map[string]string
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
t.Fatalf("decode login response: %v", err)
}
token, err := jwt.Parse(payload["accessToken"], func(token *jwt.Token) (interface{}, error) {
if token.Method != jwt.SigningMethodHS256 {
t.Fatalf("unexpected signing method: %s", token.Method.Alg())
}
return []byte(jwtSecret), nil
})
if err != nil || !token.Valid {
t.Fatalf("access token is not valid: %v", err)
}
claims := token.Claims.(jwt.MapClaims)
if claims["username"] != "admin" || claims["role"] != "admin" {
t.Fatalf("unexpected claims: %#v", claims)
}
}
func TestLoginHandlerRejectsWrongCredentials(t *testing.T) {
service := UserService{
Client: httpDoerFunc(func(req *http.Request) (*http.Response, error) {
return jsonResponse(http.StatusOK, `{"username":"admin"}`), nil
}),
UserAPIAddress: "http://users-api",
AllowedUserHashes: map[string]interface{}{},
}
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader(`{"username":"admin","password":"wrong"}`))
recorder := httptest.NewRecorder()
err := getLoginHandler(service)(e.NewContext(req, recorder))
if err != ErrWrongCredentials {
t.Fatalf("login error = %v, want %v", err, ErrWrongCredentials)
}
}
func TestLoginHandlerRejectsMalformedJSON(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader("{"))
recorder := httptest.NewRecorder()
err := getLoginHandler(UserService{})(e.NewContext(req, recorder))
if err != ErrHttpGenericMessage {
t.Fatalf("login error = %v, want %v", err, ErrHttpGenericMessage)
}
}