-
Notifications
You must be signed in to change notification settings - Fork 15
Refactoring tests in kratos package #846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shipperizer
wants to merge
2
commits into
main
Choose a base branch
from
test/kratos
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Copyright 2024 Canonical Ltd. | ||
| // SPDX-License-Identifier: AGPL-3.0 | ||
|
|
||
| // Package kratos provides unit tests for cookie management functionality. | ||
| package kratos | ||
|
|
||
| import ( | ||
|
|
@@ -29,147 +30,179 @@ func findCookie(name string, cookies []*http.Cookie) (*http.Cookie, bool) { | |
| } | ||
|
|
||
| func TestAuthCookieManager_ClearStateCookie(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
|
|
||
| mockLogger := NewMockLoggerInterface(ctrl) | ||
| mockEncrypt := NewMockEncryptInterface(ctrl) | ||
|
|
||
| mockRequest := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| mockRequest.AddCookie(&http.Cookie{Name: "state"}) | ||
|
|
||
| mockResponse := httptest.NewRecorder() | ||
|
|
||
| manager := NewAuthCookieManager(5, mockEncrypt, mockLogger) | ||
| manager.ClearStateCookie(mockResponse) | ||
|
|
||
| c, _ := findCookie("login_ui_state", mockResponse.Result().Cookies()) | ||
|
|
||
| if c.Expires != epoch { | ||
| t.Fatal("did not clear state cookie") | ||
| tests := []struct { | ||
| name string | ||
| }{ | ||
| { | ||
| name: "ClearState", | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func TestAuthCookieManager_GetStateCookie(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
|
|
||
| mockLogger := NewMockLoggerInterface(ctrl) | ||
| mockEncrypt := NewMockEncryptInterface(ctrl) | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
|
|
||
| state := FlowStateCookie{} | ||
| sj, _ := json.Marshal(state) | ||
| mockLogger := NewMockLoggerInterface(ctrl) | ||
| mockEncrypt := NewMockEncryptInterface(ctrl) | ||
|
|
||
| mockEncrypt.EXPECT().Decrypt("mock-state").Return(string(sj), nil) | ||
| mockRequest := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| mockRequest.AddCookie(&http.Cookie{Name: "state"}) | ||
|
|
||
| mockRequest := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| mockRequest.AddCookie(&http.Cookie{Name: "login_ui_state", Value: "mock-state"}) | ||
| mockResponse := httptest.NewRecorder() | ||
|
|
||
| manager := NewAuthCookieManager(5, mockEncrypt, mockLogger) | ||
| cookie, err := manager.GetStateCookie(mockRequest) | ||
| manager := NewAuthCookieManager(5, mockEncrypt, mockLogger) | ||
| manager.ClearStateCookie(mockResponse) | ||
|
|
||
| if cookie != state { | ||
| t.Fatal("state cookie value does not match expected") | ||
| } | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("expected error to be nil not %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestAuthCookieManager_GetStateCookieNoCookie(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
|
|
||
| mockLogger := NewMockLoggerInterface(ctrl) | ||
| mockRequest := httptest.NewRequest(http.MethodGet, "/", nil) | ||
|
|
||
| manager := NewAuthCookieManager(5, nil, mockLogger) | ||
| cookie, err := manager.GetStateCookie(mockRequest) | ||
|
|
||
| state := FlowStateCookie{} | ||
| if cookie != state { | ||
| t.Fatal("state cookie value does not match expected") | ||
| } | ||
| c, _ := findCookie("login_ui_state", mockResponse.Result().Cookies()) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("expected error to be nil, not %v", err) | ||
| if c.Expires != epoch { | ||
| t.Fatal("did not clear state cookie") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAuthCookieManager_GetStateCookieDecryptFailure(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| mockError := errors.New("mock-error") | ||
|
|
||
| mockLogger := NewMockLoggerInterface(ctrl) | ||
| mockLogger.EXPECT().Errorf("can't decrypt cookie value, %v", mockError).Times(1) | ||
|
|
||
| mockEncrypt := NewMockEncryptInterface(ctrl) | ||
| mockEncrypt.EXPECT().Decrypt("mock-state").Return("", mockError) | ||
|
|
||
| mockRequest := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| mockRequest.AddCookie(&http.Cookie{Name: "login_ui_state", Value: "mock-state"}) | ||
|
|
||
| manager := NewAuthCookieManager(5, mockEncrypt, mockLogger) | ||
| cookie, err := manager.GetStateCookie(mockRequest) | ||
|
|
||
| state := FlowStateCookie{} | ||
| if cookie != state { | ||
| t.Fatal("state cookie value does not match expected") | ||
| func TestAuthCookieManager_GetStateCookie(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| setupMocks func(*MockEncryptInterface, *MockLoggerInterface) | ||
| requestCookie *http.Cookie | ||
| expectedCookie FlowStateCookie | ||
| expectedErr bool | ||
| }{ | ||
| { | ||
| name: "Success", | ||
| setupMocks: func(mockEncrypt *MockEncryptInterface, mockLogger *MockLoggerInterface) { | ||
| state := FlowStateCookie{} | ||
| sj, _ := json.Marshal(state) | ||
| mockEncrypt.EXPECT().Decrypt("mock-state").Return(string(sj), nil) | ||
| }, | ||
| requestCookie: &http.Cookie{Name: "login_ui_state", Value: "mock-state"}, | ||
| expectedCookie: FlowStateCookie{}, | ||
| expectedErr: false, | ||
| }, | ||
| { | ||
| name: "NoCookie", | ||
| setupMocks: func(mockEncrypt *MockEncryptInterface, mockLogger *MockLoggerInterface) {}, | ||
| requestCookie: nil, | ||
| expectedCookie: FlowStateCookie{}, | ||
| expectedErr: false, | ||
| }, | ||
| { | ||
| name: "DecryptFailure", | ||
| setupMocks: func(mockEncrypt *MockEncryptInterface, mockLogger *MockLoggerInterface) { | ||
| mockError := errors.New("mock-error") | ||
| mockLogger.EXPECT().Errorf("can't decrypt cookie value, %v", mockError).Times(1) | ||
| mockEncrypt.EXPECT().Decrypt("mock-state").Return("", mockError) | ||
| }, | ||
| requestCookie: &http.Cookie{Name: "login_ui_state", Value: "mock-state"}, | ||
| expectedCookie: FlowStateCookie{}, | ||
| expectedErr: true, | ||
| }, | ||
| } | ||
|
|
||
| if err == nil { | ||
| t.Fatalf("expected error to be not nil") | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
|
|
||
| mockLogger := NewMockLoggerInterface(ctrl) | ||
| mockEncrypt := NewMockEncryptInterface(ctrl) | ||
|
|
||
| if tt.requestCookie == nil { | ||
| mockEncrypt = nil | ||
| } | ||
| if mockEncrypt != nil { | ||
| tt.setupMocks(mockEncrypt, mockLogger) | ||
| } else { | ||
| tt.setupMocks(nil, mockLogger) | ||
| } | ||
|
Comment on lines
+111
to
+118
|
||
|
|
||
| mockRequest := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| if tt.requestCookie != nil { | ||
| mockRequest.AddCookie(tt.requestCookie) | ||
| } | ||
|
|
||
| manager := NewAuthCookieManager(5, mockEncrypt, mockLogger) | ||
| cookie, err := manager.GetStateCookie(mockRequest) | ||
|
|
||
| if cookie != tt.expectedCookie { | ||
| t.Fatal("state cookie value does not match expected") | ||
| } | ||
|
|
||
| if tt.expectedErr { | ||
| if err == nil { | ||
| t.Fatalf("expected error to be not nil") | ||
| } | ||
| } else if err != nil { | ||
| t.Fatalf("expected error to be nil not %v", err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAuthCookieManager_SetStateCookie(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
|
|
||
| mockLogger := NewMockLoggerInterface(ctrl) | ||
| mockEncrypt := NewMockEncryptInterface(ctrl) | ||
|
|
||
| state := FlowStateCookie{} | ||
| js, _ := json.Marshal(state) | ||
|
|
||
| mockEncrypt.EXPECT().Encrypt(string(js)).Return("mock-state", nil) | ||
|
|
||
| mockResponse := httptest.NewRecorder() | ||
|
|
||
| manager := NewAuthCookieManager(5, mockEncrypt, mockLogger) | ||
| err := manager.SetStateCookie(mockResponse, state) | ||
|
|
||
| c, found := findCookie("login_ui_state", mockResponse.Result().Cookies()) | ||
|
|
||
| if !found { | ||
| t.Fatal("did not set state cookie") | ||
| tests := []struct { | ||
| name string | ||
| setupMocks func(*MockEncryptInterface, *MockLoggerInterface) | ||
| expectedErr bool | ||
| }{ | ||
| { | ||
| name: "Success", | ||
| setupMocks: func(mockEncrypt *MockEncryptInterface, mockLogger *MockLoggerInterface) { | ||
| state := FlowStateCookie{} | ||
| js, _ := json.Marshal(state) | ||
| mockEncrypt.EXPECT().Encrypt(string(js)).Return("mock-state", nil) | ||
| }, | ||
| expectedErr: false, | ||
| }, | ||
| { | ||
| name: "Failure", | ||
| setupMocks: func(mockEncrypt *MockEncryptInterface, mockLogger *MockLoggerInterface) { | ||
| mockError := errors.New("mock-error") | ||
| state := FlowStateCookie{} | ||
| js, _ := json.Marshal(state) | ||
| mockLogger.EXPECT().Errorf("can't encrypt cookie value, %v", mockError).Times(1) | ||
| mockEncrypt.EXPECT().Encrypt(string(js)).Return("", mockError) | ||
| }, | ||
| expectedErr: true, | ||
| }, | ||
| } | ||
|
|
||
| if c.Value != "mock-state" { | ||
| t.Fatal("state cookie value does not match expected") | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("expected error to be nil not %v", err) | ||
| } | ||
| } | ||
| mockLogger := NewMockLoggerInterface(ctrl) | ||
| mockEncrypt := NewMockEncryptInterface(ctrl) | ||
|
|
||
| tt.setupMocks(mockEncrypt, mockLogger) | ||
|
|
||
| func TestAuthCookieManager_SetStateCookieFailure(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| state := FlowStateCookie{} | ||
| mockResponse := httptest.NewRecorder() | ||
|
|
||
| mockError := errors.New("mock-error") | ||
| state := FlowStateCookie{} | ||
| js, _ := json.Marshal(state) | ||
| manager := NewAuthCookieManager(5, mockEncrypt, mockLogger) | ||
| err := manager.SetStateCookie(mockResponse, state) | ||
|
|
||
| mockLogger := NewMockLoggerInterface(ctrl) | ||
| mockLogger.EXPECT().Errorf("can't encrypt cookie value, %v", mockError).Times(1) | ||
| if tt.expectedErr { | ||
| if err == nil { | ||
| t.Fatalf("expected error to be not nil") | ||
| } | ||
| return | ||
| } | ||
|
|
||
| mockEncrypt := NewMockEncryptInterface(ctrl) | ||
| mockEncrypt.EXPECT().Encrypt(string(js)).Return("", mockError) | ||
| c, found := findCookie("login_ui_state", mockResponse.Result().Cookies()) | ||
|
|
||
| mockResponse := httptest.NewRecorder() | ||
| if !found { | ||
| t.Fatal("did not set state cookie") | ||
| } | ||
|
|
||
| manager := NewAuthCookieManager(5, mockEncrypt, mockLogger) | ||
| err := manager.SetStateCookie(mockResponse, state) | ||
| if c.Value != "mock-state" { | ||
| t.Fatal("state cookie value does not match expected") | ||
| } | ||
|
|
||
| if err == nil { | ||
| t.Fatalf("expected error to be not nil") | ||
| if err != nil { | ||
| t.Fatalf("expected error to be nil not %v", err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The table-driven test structure for
TestAuthCookieManager_ClearStateCookiecontains only a single test case with no variations. This adds unnecessary complexity without benefit. Either add multiple test cases to justify the table-driven approach, or revert to a simple test function.