@@ -2,6 +2,7 @@ package mfa
22
33import (
44 "bytes"
5+ "context"
56 "crypto/aes"
67 "crypto/rand"
78 "encoding/base64"
@@ -10,6 +11,7 @@ import (
1011 "fmt"
1112 "io"
1213 "net/http"
14+ "net/http/httptest"
1315 "regexp"
1416 "testing"
1517 "time"
@@ -364,78 +366,70 @@ func (ms *MfaSuite) TestAppRotateApiKey() {
364366 tests := []struct {
365367 name string
366368 body any
369+ key ApiKey
367370 wantStatus int
368- wantError error
371+ wantError string
369372 }{
370373 {
371- name : "missing oldKeyId" ,
372- body : map [string ]interface {}{
373- paramNewKeyId : newKey .Key ,
374- paramNewKeySecret : newKey .Secret ,
375- paramOldKeySecret : key .Secret ,
376- },
377- wantStatus : http .StatusBadRequest ,
378- wantError : errors .New ("oldKeyId is required" ),
379- },
380- {
381- name : "missing oldKeySecret" ,
374+ name : "missing key" ,
382375 body : map [string ]interface {}{
383376 paramNewKeyId : newKey .Key ,
384377 paramNewKeySecret : newKey .Secret ,
385- paramOldKeyId : key .Key ,
386378 },
387- wantStatus : http .StatusBadRequest ,
388- wantError : errors . New ( "oldKeySecret is required" ) ,
379+ wantStatus : http .StatusUnauthorized ,
380+ wantError : "Unauthorized" ,
389381 },
390382 {
391383 name : "missing newKeyId" ,
392384 body : map [string ]interface {}{
393385 paramNewKeySecret : newKey .Secret ,
394- paramOldKeyId : key .Key ,
395- paramOldKeySecret : key .Secret ,
396386 },
387+ key : key ,
397388 wantStatus : http .StatusBadRequest ,
398- wantError : errors . New ( "newKeyId is required" ) ,
389+ wantError : "newKeyId is required" ,
399390 },
400391 {
401392 name : "missing newKeySecret" ,
402393 body : map [string ]interface {}{
403- paramNewKeyId : newKey .Key ,
404- paramOldKeyId : key .Key ,
405- paramOldKeySecret : key .Secret ,
394+ paramNewKeyId : newKey .Key ,
406395 },
396+ key : key ,
407397 wantStatus : http .StatusBadRequest ,
408- wantError : errors . New ( "newKeySecret is required" ) ,
398+ wantError : "newKeySecret is required" ,
409399 },
410400 {
411401 name : "good" ,
412402 body : map [string ]interface {}{
413403 paramNewKeyId : newKey .Key ,
414404 paramNewKeySecret : newKey .Secret ,
415- paramOldKeyId : user .ApiKey .Key ,
416- paramOldKeySecret : key .Secret ,
417405 },
406+ key : key ,
418407 wantStatus : http .StatusOK ,
419408 },
420409 }
421410 for _ , tt := range tests {
422411 ms .Run (tt .name , func () {
423- res := & lambdaResponseWriter {Headers : http.Header {}}
424- req := requestWithUser (tt .body , key )
425- ms .app .RotateApiKey (res , req )
426-
427- if tt .wantError != nil {
428- ms .Equal (tt .wantStatus , res .Status , fmt .Sprintf ("CreateApiKey response: %s" , res .Body ))
429- var se simpleError
430- ms .decodeBody (res .Body , & se )
431- ms .ErrorIs (se , tt .wantError )
412+ jsonBody , err := json .Marshal (tt .body )
413+ must (err )
414+ b := io .NopCloser (bytes .NewReader (jsonBody ))
415+ request , _ := http .NewRequest (http .MethodPost , "/api-key/rotate" , b )
416+ request .Header .Set (HeaderAPIKey , tt .key .Key )
417+ request .Header .Set (HeaderAPISecret , tt .key .Secret )
418+
419+ ctxWithUser := context .WithValue (request .Context (), UserContextKey , tt .key )
420+ request = request .WithContext (ctxWithUser )
421+
422+ res := httptest .NewRecorder ()
423+ Router (ms .app ).ServeHTTP (res , request )
424+ ms .Equal (tt .wantStatus , res .Code , "incorrect http status, body: %s" , res .Body .String ())
425+
426+ if tt .wantError != "" {
427+ ms .Contains (res .Body .String (), tt .wantError )
432428 return
433429 }
434430
435- ms .Equal (tt .wantStatus , res .Status , fmt .Sprintf ("CreateApiKey response: %s" , res .Body ))
436-
437431 var response map [string ]int
438- ms .decodeBody (res .Body , & response )
432+ ms .decodeBody (res .Body . Bytes () , & response )
439433 ms .Equal (1 , response ["totpComplete" ])
440434 ms .Equal (1 , response ["webauthnComplete" ])
441435
0 commit comments