@@ -5,11 +5,13 @@ import (
5
5
"crypto/rand"
6
6
"encoding/json"
7
7
"errors"
8
+ "fmt"
8
9
"mime/multipart"
9
10
"net/http"
10
11
"net/http/httptest"
11
12
"testing"
12
13
14
+ "github.com/go-chi/chi"
13
15
"github.com/golang/mock/gomock"
14
16
"github.com/google/uuid"
15
17
"github.com/samber/do"
@@ -258,3 +260,84 @@ func TestAddDataError(t *testing.T) {
258
260
status , http .StatusOK )
259
261
}
260
262
}
263
+
264
+ func TestDeleteKey (t * testing.T ) {
265
+ // Arrange
266
+ keyId := uuid .New ()
267
+ req := httptest .NewRequest ("DELETE" , fmt .Sprintf ("/%s" , keyId .String ()), nil )
268
+ user := testutils .GenerateUser ()
269
+ req = testutils .AddUserContext (req , user )
270
+ key := & models.SshKey {
271
+ ID : keyId ,
272
+ UserID : user .ID ,
273
+ }
274
+
275
+ injector := do .New ()
276
+ ctrl := gomock .NewController (t )
277
+ defer ctrl .Finish ()
278
+ mockUserRepo := repository .NewMockUserRepository (ctrl )
279
+ txMock := pgx .NewMockTx (ctrl )
280
+ mockUserRepo .EXPECT ().GetUserKey (user .ID , keyId ).Return (key , nil )
281
+ mockUserRepo .EXPECT ().DeleteUserKeyTx (gomock .Any (), keyId , txMock ).Return (nil )
282
+ do .Provide (injector , func (i * do.Injector ) (repository.UserRepository , error ) {
283
+ return mockUserRepo , nil
284
+ })
285
+ mockTransactionService := query .NewMockTransactionService (ctrl )
286
+ mockTransactionService .EXPECT ().StartTx (gomock .Any ()).Return (txMock , nil )
287
+ mockTransactionService .EXPECT ().Commit (txMock ).Return (nil )
288
+ do .Provide (injector , func (i * do.Injector ) (query.TransactionService , error ) {
289
+ return mockTransactionService , nil
290
+ })
291
+ // Act
292
+ rr := httptest .NewRecorder ()
293
+ handler := chi .NewRouter ()
294
+ handler .Delete ("/{id}" , deleteData (injector ))
295
+ handler .ServeHTTP (rr , req )
296
+ // Assert
297
+ if status := rr .Code ; status != http .StatusOK {
298
+ t .Errorf ("deleteData returned wrong status code: got %v want %v" ,
299
+ status , http .StatusOK )
300
+ }
301
+ }
302
+
303
+ func TestDeleteKeyError (t * testing.T ) {
304
+ // Arrange
305
+ keyId := uuid .New ()
306
+ req := httptest .NewRequest ("DELETE" , fmt .Sprintf ("/%s" , keyId .String ()), nil )
307
+ user := testutils .GenerateUser ()
308
+ req = testutils .AddUserContext (req , user )
309
+ key := & models.SshKey {
310
+ ID : keyId ,
311
+ UserID : user .ID ,
312
+ }
313
+
314
+ injector := do .New ()
315
+ ctrl := gomock .NewController (t )
316
+ defer ctrl .Finish ()
317
+ mockUserRepo := repository .NewMockUserRepository (ctrl )
318
+ txMock := pgx .NewMockTx (ctrl )
319
+ mockUserRepo .EXPECT ().GetUserKey (user .ID , keyId ).Return (key , nil )
320
+ mockUserRepo .EXPECT ().DeleteUserKeyTx (gomock .Any (), keyId , txMock ).Return (errors .New ("error" ))
321
+ do .Provide (injector , func (i * do.Injector ) (repository.UserRepository , error ) {
322
+ return mockUserRepo , nil
323
+ })
324
+ mockTransactionService := query .NewMockTransactionService (ctrl )
325
+ mockTransactionService .EXPECT ().StartTx (gomock .Any ()).Return (txMock , nil )
326
+ mockTransactionService .EXPECT ().Rollback (txMock ).Return (nil )
327
+ do .Provide (injector , func (i * do.Injector ) (query.TransactionService , error ) {
328
+ return mockTransactionService , nil
329
+ })
330
+
331
+ // Act
332
+ rr := httptest .NewRecorder ()
333
+ handler := chi .NewRouter ()
334
+ handler .Delete ("/{id}" , deleteData (injector ))
335
+ handler .ServeHTTP (rr , req )
336
+
337
+ // Assert
338
+
339
+ if status := rr .Code ; status != http .StatusInternalServerError {
340
+ t .Errorf ("deleteData returned wrong status code: got %v want %v" ,
341
+ status , http .StatusInternalServerError )
342
+ }
343
+ }
0 commit comments