@@ -88,7 +88,7 @@ func TestUserController_Register(t *testing.T) {
88
88
89
89
func TestUserController_Login (t * testing.T ) {
90
90
// Setup form data
91
- data := strings .NewReader ("email=govel63bf26a363c2a @gmail.com&password=rahasia" )
91
+ data := strings .NewReader ("email=consequatur @gmail.com&password=rahasia" )
92
92
93
93
// Setup request
94
94
request := httptest .NewRequest ("POST" , "/api/v1/users/login" , data )
@@ -124,6 +124,48 @@ func TestUserController_Login(t *testing.T) {
124
124
json .Unmarshal (jsonClaims , & loginUserResponse )
125
125
assert .NotEmpty (t , loginUserResponse .Name )
126
126
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 )
127
169
}
128
170
129
171
func TestUserController_Show (t * testing.T ) {
@@ -152,10 +194,11 @@ func TestUserController_Show(t *testing.T) {
152
194
153
195
func TestUserController_Update (t * testing.T ) {
154
196
// 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" )
156
199
157
200
// Setup request
158
- request := httptest .NewRequest ("POST" , "/api/v1/users/update/3 " , data )
201
+ request := httptest .NewRequest ("POST" , "/api/v1/users/update/11 " , data )
159
202
160
203
// Setup header
161
204
request .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
@@ -183,8 +226,11 @@ func TestUserController_Update(t *testing.T) {
183
226
}
184
227
185
228
func TestUserController_Delete (t * testing.T ) {
229
+ // Setup form data
230
+ data := strings .NewReader ("token=eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwic29jaWFsX2lkIjoiIiwiZW1haWwiOiJxdWlAZ21haWwuY29tIiwibmljayI6InNpbnQiLCJuYW1lIjoibWluaW1hIiwicGljIjoiL2Fzc2V0cy9zdGF0aWMvdXNlci5wbmciLCJsb2NhdGlvbiI6IkluZG9uZXNpYSIsImRlc2MiOiIiLCJyb2xlIjoxfQ.fAT_hDCcUq7eL7CfI-Z6UIOhxTChJeeScR9-BcaoA86nxw_7z5RuSfZa59DFRJApabm-s1TeFSE92aw4w2pwng" )
231
+
186
232
// Setup request
187
- request := httptest .NewRequest ("POST" , "/api/v1/users/delete/1" , nil )
233
+ request := httptest .NewRequest ("POST" , "/api/v1/users/delete/1" , data )
188
234
189
235
// Setup header
190
236
request .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
0 commit comments