Skip to content

Commit c6f56f8

Browse files
committed
add refresh token test
1 parent 728c74e commit c6f56f8

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

test/user_controller_test.go

+50-4
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestUserController_Register(t *testing.T) {
8888

8989
func TestUserController_Login(t *testing.T) {
9090
// Setup form data
91-
data := strings.NewReader("email=govel63bf26a363c2a@gmail.com&password=rahasia")
91+
data := strings.NewReader("email=consequatur@gmail.com&password=rahasia")
9292

9393
// Setup request
9494
request := httptest.NewRequest("POST", "/api/v1/users/login", data)
@@ -124,6 +124,48 @@ func TestUserController_Login(t *testing.T) {
124124
json.Unmarshal(jsonClaims, &loginUserResponse)
125125
assert.NotEmpty(t, loginUserResponse.Name)
126126
assert.Equal(t, request.FormValue("email"), loginUserResponse.Email)
127+
assert.Equal(t, 1, loginUserResponse.Role)
128+
}
129+
130+
func TestUserController_RefreshToken(t *testing.T) {
131+
// Setup form data
132+
data := strings.NewReader("token=eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTEsInNvY2lhbF9pZCI6IiIsImVtYWlsIjoiY29uc2VxdWF0dXJAZ21haWwuY29tIiwibmljayI6Im9jY2FlY2F0aSIsIm5hbWUiOiJlaXVzIiwicGljIjoiL2Fzc2V0cy9zdGF0aWMvdXNlci5wbmciLCJsb2NhdGlvbiI6IkluZG9uZXNpYSIsImRlc2MiOiIiLCJyb2xlIjoxfQ.8SoZMH7f_iReJ-YQmks92QAXo_TjRzrO3xtsdnD_6bvFcJiwXiBSmr1D_7oHEtjdkHNJpeWGCy_yhDiTlzZSXQ")
133+
134+
// Setup request
135+
request := httptest.NewRequest("POST", "/api/v1/users/refresh-token", data)
136+
137+
// Setup header
138+
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
139+
request.Header.Set("Accept", "application/x-www-form-urlencoded")
140+
141+
// Test the request
142+
response, _ := app.Test(request)
143+
// assert.Equal(t, 200, response.StatusCode)
144+
145+
// Test default json result
146+
responseBody, _ := io.ReadAll(response.Body)
147+
webResponse := model.WebResponse{}
148+
json.Unmarshal(responseBody, &webResponse)
149+
assert.Equal(t, 200, webResponse.Code)
150+
assert.Equal(t, "OK", webResponse.Message)
151+
152+
// Test response data
153+
jsonData, _ := json.Marshal(webResponse.Data)
154+
tokenResponse := model.TokenResponse{}
155+
json.Unmarshal(jsonData, &tokenResponse)
156+
assert.Equal(t, "bearer", tokenResponse.Type)
157+
assert.Equal(t, "es256", tokenResponse.Alg)
158+
159+
// Check token is valid
160+
token := helper.ParseECDSAToken(tokenResponse.Token, jwt.SigningMethodES256)
161+
assert.True(t, token.Valid)
162+
163+
jsonClaims, _ := json.Marshal(tokenResponse.Claims)
164+
refreshTokenUserResponse := model.RefreshTokenUserResponse{}
165+
json.Unmarshal(jsonClaims, &refreshTokenUserResponse)
166+
assert.NotEmpty(t, refreshTokenUserResponse.Name)
167+
assert.NotEmpty(t, refreshTokenUserResponse.Email)
168+
assert.Equal(t, 1, refreshTokenUserResponse.Role)
127169
}
128170

129171
func TestUserController_Show(t *testing.T) {
@@ -152,10 +194,11 @@ func TestUserController_Show(t *testing.T) {
152194

153195
func TestUserController_Update(t *testing.T) {
154196
// Setup form data
155-
data := strings.NewReader("name=Saiful Wicaksana&location=Jakarta&desc=Engineer")
197+
token := "token=eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTEsInNvY2lhbF9pZCI6IiIsImVtYWlsIjoiY29uc2VxdWF0dXJAZ21haWwuY29tIiwibmljayI6Im9jY2FlY2F0aSIsIm5hbWUiOiJlaXVzIiwicGljIjoiL2Fzc2V0cy9zdGF0aWMvdXNlci5wbmciLCJsb2NhdGlvbiI6IkluZG9uZXNpYSIsImRlc2MiOiIiLCJyb2xlIjoxfQ.8SoZMH7f_iReJ-YQmks92QAXo_TjRzrO3xtsdnD_6bvFcJiwXiBSmr1D_7oHEtjdkHNJpeWGCy_yhDiTlzZSXQ"
198+
data := strings.NewReader(token + "&name=Saiful Wicaksana&location=Jakarta&desc=Engineer")
156199

157200
// Setup request
158-
request := httptest.NewRequest("POST", "/api/v1/users/update/3", data)
201+
request := httptest.NewRequest("POST", "/api/v1/users/update/11", data)
159202

160203
// Setup header
161204
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
@@ -183,8 +226,11 @@ func TestUserController_Update(t *testing.T) {
183226
}
184227

185228
func TestUserController_Delete(t *testing.T) {
229+
// Setup form data
230+
data := strings.NewReader("token=eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwic29jaWFsX2lkIjoiIiwiZW1haWwiOiJxdWlAZ21haWwuY29tIiwibmljayI6InNpbnQiLCJuYW1lIjoibWluaW1hIiwicGljIjoiL2Fzc2V0cy9zdGF0aWMvdXNlci5wbmciLCJsb2NhdGlvbiI6IkluZG9uZXNpYSIsImRlc2MiOiIiLCJyb2xlIjoxfQ.fAT_hDCcUq7eL7CfI-Z6UIOhxTChJeeScR9-BcaoA86nxw_7z5RuSfZa59DFRJApabm-s1TeFSE92aw4w2pwng")
231+
186232
// Setup request
187-
request := httptest.NewRequest("POST", "/api/v1/users/delete/1", nil)
233+
request := httptest.NewRequest("POST", "/api/v1/users/delete/1", data)
188234

189235
// Setup header
190236
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")

0 commit comments

Comments
 (0)