|
|
|
@ -201,6 +201,112 @@ func TestAPIKey_Test(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestAPIKey_GetAPIKeyIDFromIdentity(t *testing.T) { |
|
|
|
|
type TestCase struct { |
|
|
|
|
desc string |
|
|
|
|
expectedKey *apikey.APIKey |
|
|
|
|
expectedIdentity *authn.Identity |
|
|
|
|
expectedError error |
|
|
|
|
expectedKeyID int64 |
|
|
|
|
expectedExists bool |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tests := []TestCase{ |
|
|
|
|
{ |
|
|
|
|
desc: "should return API Key ID for valid token that is connected to service account", |
|
|
|
|
expectedKey: &apikey.APIKey{ |
|
|
|
|
ID: 1, |
|
|
|
|
OrgID: 1, |
|
|
|
|
Key: hash, |
|
|
|
|
ServiceAccountId: intPtr(1), |
|
|
|
|
}, |
|
|
|
|
expectedIdentity: &authn.Identity{ |
|
|
|
|
ID: "service-account:1", |
|
|
|
|
OrgID: 1, |
|
|
|
|
Name: "test", |
|
|
|
|
AuthenticatedBy: login.APIKeyAuthModule, |
|
|
|
|
}, |
|
|
|
|
expectedKeyID: 1, |
|
|
|
|
expectedExists: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
desc: "should return API Key ID for valid token for API key", |
|
|
|
|
expectedKey: &apikey.APIKey{ |
|
|
|
|
ID: 2, |
|
|
|
|
OrgID: 1, |
|
|
|
|
Key: hash, |
|
|
|
|
}, |
|
|
|
|
expectedIdentity: &authn.Identity{ |
|
|
|
|
ID: "api-key:2", |
|
|
|
|
OrgID: 1, |
|
|
|
|
Name: "test", |
|
|
|
|
AuthenticatedBy: login.APIKeyAuthModule, |
|
|
|
|
}, |
|
|
|
|
expectedKeyID: 2, |
|
|
|
|
expectedExists: true, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
desc: "should not return any ID when the request is not made by API key or service account", |
|
|
|
|
expectedKey: &apikey.APIKey{ |
|
|
|
|
ID: 2, |
|
|
|
|
OrgID: 1, |
|
|
|
|
Key: hash, |
|
|
|
|
}, |
|
|
|
|
expectedIdentity: &authn.Identity{ |
|
|
|
|
ID: "user:2", |
|
|
|
|
OrgID: 1, |
|
|
|
|
Name: "test", |
|
|
|
|
AuthenticatedBy: login.APIKeyAuthModule, |
|
|
|
|
}, |
|
|
|
|
expectedKeyID: -1, |
|
|
|
|
expectedExists: false, |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
desc: "should not return any ID when the can't fetch API Key", |
|
|
|
|
expectedKey: &apikey.APIKey{ |
|
|
|
|
ID: 1, |
|
|
|
|
OrgID: 1, |
|
|
|
|
Key: hash, |
|
|
|
|
}, |
|
|
|
|
expectedIdentity: &authn.Identity{ |
|
|
|
|
ID: "service-account:2", |
|
|
|
|
OrgID: 1, |
|
|
|
|
Name: "test", |
|
|
|
|
AuthenticatedBy: login.APIKeyAuthModule, |
|
|
|
|
}, |
|
|
|
|
expectedError: fmt.Errorf("invalid token"), |
|
|
|
|
expectedKeyID: -1, |
|
|
|
|
expectedExists: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
req := &authn.Request{HTTPRequest: &http.Request{ |
|
|
|
|
Header: map[string][]string{ |
|
|
|
|
"Authorization": {"Bearer " + secret}, |
|
|
|
|
}, |
|
|
|
|
}} |
|
|
|
|
|
|
|
|
|
signedInUser := &user.SignedInUser{ |
|
|
|
|
UserID: 1, |
|
|
|
|
OrgID: 1, |
|
|
|
|
Name: "test", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, tt := range tests { |
|
|
|
|
t.Run(tt.desc, func(t *testing.T) { |
|
|
|
|
c := ProvideAPIKey(&apikeytest.Service{ |
|
|
|
|
ExpectedError: tt.expectedError, |
|
|
|
|
ExpectedAPIKey: tt.expectedKey, |
|
|
|
|
}, &usertest.FakeUserService{ |
|
|
|
|
ExpectedSignedInUser: signedInUser, |
|
|
|
|
}) |
|
|
|
|
id, exists := c.getAPIKeyID(context.Background(), tt.expectedIdentity, req) |
|
|
|
|
assert.Equal(t, tt.expectedExists, exists) |
|
|
|
|
assert.Equal(t, tt.expectedKeyID, id) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func intPtr(n int64) *int64 { |
|
|
|
|
return &n |
|
|
|
|
} |
|
|
|
|