forked from CeoFred/fast-otp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastotp_test.go
120 lines (104 loc) · 2.93 KB
/
fastotp_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
package fastotp
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestGenerateOTP(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Expected POST request, got %s", r.Method)
}
if r.URL.Path != "/generate" {
t.Errorf("Expected path /generate, got %s", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{
"otp": {
"created_at": "2024-01-19T00:24:06.000000Z",
"delivery_details": {
"email": "[email protected]"
},
"delivery_methods": [
"email"
],
"expires_at": "2024-01-19T17:04:06.000000Z",
"id": "9b202659-fee7-46ab-836b-cdd310c4f327",
"identifier": "test_identifier",
"status": "pending",
"type": "alpha_numeric",
"updated_at": "2024-01-19T00:24:06.000000Z"
}
}`))
body, _ := io.ReadAll(r.Body)
fmt.Println("Request Body:", string(body))
}))
defer server.Close()
fastOtp := &FastOtp{APIKey: new(string), BaseURL: server.URL}
delivery := OtpDelivery{
"email": "[email protected]",
}
payload := GenerateOTPPayload{
Delivery: delivery,
Identifier: "test_identifier",
TokenLength: 6,
Type: "alpha_numeric",
Validity: 120,
}
otp, err := fastOtp.GenerateOTP(payload)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
fmt.Println("Generated OTP:", otp)
if otp.DeliveryDetails.Email != "[email protected]" {
t.Errorf("Expected email: %s, got %s", "[email protected]", otp.DeliveryDetails.Email)
}
if otp.Identifier != "test_identifier" {
t.Errorf("Expected identifier: %s, got %s", "test_identifier", otp.Identifier)
}
}
func TestValidateOTP(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Expected POST request, got %s", r.Method)
}
if r.URL.Path != "/validate" {
t.Errorf("Expected path /validate, got %s", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{
"otp": {
"created_at": "2024-01-19T00:24:06.000000Z",
"delivery_details": {
"email": "[email protected]"
},
"delivery_methods": [
"email"
],
"expires_at": "2024-01-19T17:04:06.000000Z",
"id": "9b202659-fee7-46ab-836b-cdd310c4f327",
"identifier": "test_identifier",
"status": "validated",
"type": "alpha_numeric",
"updated_at": "2024-01-19T00:24:06.000000Z"
}
}`))
body, _ := io.ReadAll(r.Body)
fmt.Println("Request Body:", string(body))
}))
defer server.Close()
fastOtp := &FastOtp{APIKey: new(string), BaseURL: server.URL}
payload := ValidateOTPPayload{
Identifier: "test_identifier",
Token: "123456",
}
otp, err := fastOtp.ValidateOTP(payload)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if otp.Status != "validated" {
t.Errorf("Expected OTP status to be: %s, got %s", "validated", otp.DeliveryDetails.Email)
}
}