Skip to content

Commit

Permalink
make static definition of expected error into testCase field
Browse files Browse the repository at this point in the history
  • Loading branch information
henryluo committed Oct 17, 2024
1 parent 4a09a47 commit a3cd3b1
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion server/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ func TestAuthorizeRequest(t *testing.T) {
Input input
CallBackImpl func(input knox.AccessCallbackInput) (bool, error)
ExpectedAuthorized bool
ExpectedError error
}

triggerCallBackInput := input{
Expand All @@ -637,6 +638,7 @@ func TestAuthorizeRequest(t *testing.T) {
return true, nil
},
ExpectedAuthorized: true,
ExpectedError: nil,
},
{
Name: "AccessCallback returns false",
Expand All @@ -645,6 +647,7 @@ func TestAuthorizeRequest(t *testing.T) {
return false, nil
},
ExpectedAuthorized: false,
ExpectedError: nil,
},
{
Name: "AccessCallback returns false with valid input",
Expand All @@ -653,12 +656,14 @@ func TestAuthorizeRequest(t *testing.T) {
return input.AccessType == knox.Write && input.Key.ACL[0].ID == "test" && input.Key.ACL[0].Type == knox.User, nil
},
ExpectedAuthorized: true,
ExpectedError: nil,
},
{
Name: "AccessCallback is nil",
Input: triggerCallBackInput,
CallBackImpl: nil,
ExpectedAuthorized: false,
ExpectedError: nil,
},
{
Name: "AccessCallback panics",
Expand All @@ -667,6 +672,7 @@ func TestAuthorizeRequest(t *testing.T) {
panic("intentional panic")
},
ExpectedAuthorized: false,
ExpectedError: fmt.Errorf("Recovered from panic in access callback: intentional panic"),
},
}

Expand All @@ -677,7 +683,7 @@ func TestAuthorizeRequest(t *testing.T) {
SetAccessCallback(tc.CallBackImpl)
authorized, err := authorizeRequest(tc.Input.Key, tc.Input.Principal, tc.Input.AccessType)
if err != nil {
if err.Error() == "Recovered from panic in access callback: intentional panic" {
if err.Error() == tc.ExpectedError.Error() {
if authorized != tc.ExpectedAuthorized {
t.Fatalf("Expected %v, got %v", tc.ExpectedAuthorized, authorized)
}
Expand Down

0 comments on commit a3cd3b1

Please sign in to comment.