@@ -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"
@@ -372,78 +374,70 @@ func (ms *MfaSuite) TestAppRotateApiKey() {
372374 tests := []struct {
373375 name string
374376 body any
377+ key ApiKey
375378 wantStatus int
376- wantError error
379+ wantError string
377380 }{
378381 {
379- name : "missing oldKeyId" ,
380- body : map [string ]interface {}{
381- paramNewKeyId : newKey .Key ,
382- paramNewKeySecret : newKey .Secret ,
383- paramOldKeySecret : key .Secret ,
384- },
385- wantStatus : http .StatusBadRequest ,
386- wantError : errors .New ("oldKeyId is required" ),
387- },
388- {
389- name : "missing oldKeySecret" ,
382+ name : "missing key" ,
390383 body : map [string ]interface {}{
391384 paramNewKeyId : newKey .Key ,
392385 paramNewKeySecret : newKey .Secret ,
393- paramOldKeyId : key .Key ,
394386 },
395- wantStatus : http .StatusBadRequest ,
396- wantError : errors . New ( "oldKeySecret is required" ) ,
387+ wantStatus : http .StatusUnauthorized ,
388+ wantError : "Unauthorized" ,
397389 },
398390 {
399391 name : "missing newKeyId" ,
400392 body : map [string ]interface {}{
401393 paramNewKeySecret : newKey .Secret ,
402- paramOldKeyId : key .Key ,
403- paramOldKeySecret : key .Secret ,
404394 },
395+ key : key ,
405396 wantStatus : http .StatusBadRequest ,
406- wantError : errors . New ( "newKeyId is required" ) ,
397+ wantError : "newKeyId is required" ,
407398 },
408399 {
409400 name : "missing newKeySecret" ,
410401 body : map [string ]interface {}{
411- paramNewKeyId : newKey .Key ,
412- paramOldKeyId : key .Key ,
413- paramOldKeySecret : key .Secret ,
402+ paramNewKeyId : newKey .Key ,
414403 },
404+ key : key ,
415405 wantStatus : http .StatusBadRequest ,
416- wantError : errors . New ( "newKeySecret is required" ) ,
406+ wantError : "newKeySecret is required" ,
417407 },
418408 {
419409 name : "good" ,
420410 body : map [string ]interface {}{
421411 paramNewKeyId : newKey .Key ,
422412 paramNewKeySecret : newKey .Secret ,
423- paramOldKeyId : user .ApiKey .Key ,
424- paramOldKeySecret : key .Secret ,
425413 },
414+ key : key ,
426415 wantStatus : http .StatusOK ,
427416 },
428417 }
429418 for _ , tt := range tests {
430419 ms .Run (tt .name , func () {
431- res := & lambdaResponseWriter {Headers : http.Header {}}
432- req := requestWithUser (tt .body , key )
433- ms .app .RotateApiKey (res , req )
434-
435- if tt .wantError != nil {
436- ms .Equal (tt .wantStatus , res .Status , fmt .Sprintf ("CreateApiKey response: %s" , res .Body ))
437- var se simpleError
438- ms .decodeBody (res .Body , & se )
439- ms .ErrorIs (se , tt .wantError )
420+ jsonBody , err := json .Marshal (tt .body )
421+ must (err )
422+ b := io .NopCloser (bytes .NewReader (jsonBody ))
423+ request , _ := http .NewRequest (http .MethodPost , "/api-key/rotate" , b )
424+ request .Header .Set (HeaderAPIKey , tt .key .Key )
425+ request .Header .Set (HeaderAPISecret , tt .key .Secret )
426+
427+ ctxWithUser := context .WithValue (request .Context (), UserContextKey , tt .key )
428+ request = request .WithContext (ctxWithUser )
429+
430+ res := httptest .NewRecorder ()
431+ Router (ms .app ).ServeHTTP (res , request )
432+ ms .Equal (tt .wantStatus , res .Code , "incorrect http status, body: %s" , res .Body .String ())
433+
434+ if tt .wantError != "" {
435+ ms .Contains (res .Body .String (), tt .wantError )
440436 return
441437 }
442438
443- ms .Equal (tt .wantStatus , res .Status , fmt .Sprintf ("CreateApiKey response: %s" , res .Body ))
444-
445439 var response map [string ]int
446- ms .decodeBody (res .Body , & response )
440+ ms .decodeBody (res .Body . Bytes () , & response )
447441 ms .Equal (1 , response ["totpComplete" ])
448442 ms .Equal (1 , response ["webauthnComplete" ])
449443
0 commit comments