-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp_test.go
125 lines (114 loc) · 3.78 KB
/
otp_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
package magiclinksdev_test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
mld "github.com/MicahParks/magiclinksdev"
"github.com/MicahParks/magiclinksdev/model"
"github.com/MicahParks/magiclinksdev/network"
"github.com/MicahParks/magiclinksdev/network/middleware"
)
func TestOTP(t *testing.T) {
for _, tc := range []struct {
name string
reqBody model.OTPCreateRequest
}{
{
name: "NumericDefault",
reqBody: model.OTPCreateRequest{
OTPCreateParams: model.OTPCreateParams{
CharSetAlphaLower: false,
CharSetAlphaUpper: false,
CharSetNumeric: true,
Length: 0,
LifespanSeconds: 0,
},
},
},
{
name: "AllLong",
reqBody: model.OTPCreateRequest{
OTPCreateParams: model.OTPCreateParams{
CharSetAlphaLower: true,
CharSetAlphaUpper: true,
CharSetNumeric: true,
Length: 12,
LifespanSeconds: 0,
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
marshaled, err := json.Marshal(tc.reqBody)
if err != nil {
t.Fatalf("Failed to marshal request body: %v", err)
}
recorder := httptest.NewRecorder()
u, err := assets.conf.Server.BaseURL.Get().Parse(network.PathOTPCreate)
if err != nil {
t.Fatalf("Failed to parse URL: %v", err)
}
req := httptest.NewRequest(http.MethodPost, u.Path, bytes.NewReader(marshaled))
req.Header.Set(mld.HeaderContentType, mld.ContentTypeJSON)
req.Header.Set(middleware.APIKeyHeader, assets.sa.APIKey.String())
assets.mux.ServeHTTP(recorder, req)
if recorder.Code != http.StatusCreated {
t.Fatalf("Received non-200 status code: %d\n%s", recorder.Code, recorder.Body.String())
}
if recorder.Header().Get(mld.HeaderContentType) != mld.ContentTypeJSON {
t.Fatalf("Received non-JSON content type: %s", recorder.Header().Get(mld.HeaderContentType))
}
var optCreateResponse model.OTPCreateResponse
err = json.Unmarshal(recorder.Body.Bytes(), &optCreateResponse)
if err != nil {
t.Fatalf("Failed to unmarshal response body: %v", err)
}
recorder = httptest.NewRecorder()
u, err = assets.conf.Server.BaseURL.Get().Parse(network.PathOTPValidate)
if err != nil {
t.Fatalf("Failed to parse URL: %v", err)
}
body := model.OTPValidateRequest{
OTPValidateParams: model.OTPValidateParams{
ID: optCreateResponse.OTPCreateResults.ID,
OTP: optCreateResponse.OTPCreateResults.OTP,
},
}
marshaled, err = json.Marshal(body)
if err != nil {
t.Fatalf("Failed to marshal request body: %v", err)
}
req = httptest.NewRequest(http.MethodPost, u.String(), bytes.NewReader(marshaled))
req.Header.Set(mld.HeaderContentType, mld.ContentTypeJSON)
req.Header.Set(middleware.APIKeyHeader, assets.sa.APIKey.String())
assets.mux.ServeHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("Expected status code %d, got %d", http.StatusOK, recorder.Code)
}
recorder = httptest.NewRecorder()
u, err = assets.conf.Server.BaseURL.Get().Parse(network.PathOTPValidate)
if err != nil {
t.Fatalf("Failed to parse URL: %v", err)
}
body = model.OTPValidateRequest{
OTPValidateParams: model.OTPValidateParams{
ID: optCreateResponse.OTPCreateResults.ID,
OTP: optCreateResponse.OTPCreateResults.OTP,
},
}
marshaled, err = json.Marshal(body)
if err != nil {
t.Fatalf("Failed to marshal request body: %v", err)
}
req = httptest.NewRequest(http.MethodPost, u.String(), bytes.NewReader(marshaled))
req.Header.Set(mld.HeaderContentType, mld.ContentTypeJSON)
req.Header.Set(middleware.APIKeyHeader, assets.sa.APIKey.String())
assets.mux.ServeHTTP(recorder, req)
if recorder.Code != http.StatusBadRequest {
t.Fatalf("Expected status code %d, got %d", http.StatusBadRequest, recorder.Code)
}
})
}
}