-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauth_integration_test.go
More file actions
156 lines (121 loc) · 3.38 KB
/
Copy pathauth_integration_test.go
File metadata and controls
156 lines (121 loc) · 3.38 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package tests
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"testing"
"time"
)
func Test_Auth(t *testing.T) {
host, port := os.Getenv("HOST"), os.Getenv("PORT")
fmt.Printf("%s %s", host, port)
if host == "" {
host = "localhost"
}
if port == "" {
port = "8080"
}
baseURL := fmt.Sprintf("http://%s:%s", host, port)
uniqueUsername := fmt.Sprintf("user%d", time.Now().Unix())
password := "SecurePassword123!"
client := &http.Client{
Timeout: 5 * time.Second,
}
var fullUsername string
t.Run("Register New User", func(t *testing.T) {
regPayLoad := map[string]string{
"username": uniqueUsername,
"display_name": "Tester ABC",
"password": password,
}
body, err := json.Marshal(regPayLoad)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
resp, err := client.Post(baseURL+"/api/auth/register", "application/json", bytes.NewBuffer(body))
if err != nil {
t.Fatalf("Failed to hit register endpoint at %s: %v", baseURL, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
t.Fatalf("Expected 201 on registration, got %d", resp.StatusCode)
}
var regResp struct {
Data struct {
User struct {
Username string `json:"username"`
} `json:"user"`
} `json:"data"`
}
if err:= json.NewDecoder(resp.Body).Decode(®Resp);
err != nil {
t.Fatalf("Failed to decode register: %v", err)
}
if regResp.Data.User.Username == "" {
t.Fatalf("response did not return a username")
}
fullUsername = regResp.Data.User.Username
})
time.Sleep(1*time.Second)
var accessToken string
var refreshToken string
t.Run("Login User", func(t *testing.T) {
loginPayLoad := map[string]string{
"username": fullUsername,
"password": password,
}
body, err := json.Marshal(loginPayLoad)
if err != nil {
t.Fatalf("Marshal Failed: %v", err)
}
resp, err := client.Post(baseURL+"/api/auth/login", "application/json", bytes.NewBuffer(body))
if err != nil {
t.Fatalf("Failed to hit login endpoint: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected 200 OK on login, got: %d", resp.StatusCode)
}
var loginResp struct {
Success bool `json:"success"`
Data struct {
Tokens struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
} `json:"tokens"`
} `json:"data"`
}
err = json.NewDecoder(resp.Body).Decode(&loginResp)
if err != nil {
t.Fatalf("Failed to parse login token, Response: %v", err)
}
accessToken = loginResp.Data.Tokens.AccessToken
refreshToken = loginResp.Data.Tokens.RefreshToken
if accessToken == "" {
t.Fatalf("empty access token returned")
}
if refreshToken == "" {
t.Fatalf("empty refresh token returned")
}
})
t.Run("Refresh Token", func(t *testing.T) {
if refreshToken == "" {
t.Skip("Skippping token refresh as login failed to capture token")
}
refPayLoad := map[string]string{
"refresh_token": refreshToken,
}
body, _ := json.Marshal(refPayLoad)
resp, err := client.Post(baseURL+"/api/auth/refresh", "application/json", bytes.NewBuffer(body))
if err != nil {
t.Fatalf("Failed to hit refresh endpoint: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected 200 OK on token refresh, got: %d", resp.StatusCode)
}
})
// have to add function to delete created user
}