Skip to content
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

allow nil values for consumer and credential #153

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on:

jobs:
tests:
timeout-minutes: ${{ fromJSON(vars.GHA_DEFAULT_TIMEOUT) }}
name: Tests
runs-on: ubuntu-20.04

Expand Down
22 changes: 12 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,27 @@ func (c Client) GetConsumer() (consumer entities.Consumer, err error) {
// for the current request. While both consumer and credential can be nil,
// it is required that at least one of them exists. Otherwise this function will throw an error.
func (c Client) Authenticate(consumer *entities.Consumer, credential *AuthenticatedCredential) error {
if consumer == nil {
return fmt.Errorf("Invalid consumer")
if consumer == nil && credential == nil {
return fmt.Errorf("either credential or consumer must be provided")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice that you preserved the kong error message here : )

}
if credential == nil {
return fmt.Errorf("Invalid credential")
}
arg := &kong_plugin_protocol.AuthenticateArgs{
Consumer: &kong_plugin_protocol.Consumer{

arg := &kong_plugin_protocol.AuthenticateArgs{}
if consumer != nil {
arg.Consumer = &kong_plugin_protocol.Consumer{
Id: consumer.Id,
CreatedAt: int64(consumer.CreatedAt),
Username: consumer.Username,
CustomId: consumer.CustomId,
Tags: consumer.Tags,
},
Credential: &kong_plugin_protocol.AuthenticatedCredential{
}
}
if credential != nil {
arg.Credential = &kong_plugin_protocol.AuthenticatedCredential{
Id: credential.Id,
ConsumerId: credential.ConsumerId,
},
}
}

err := c.Ask(`kong.client.authenticate`, arg, nil)
return err
}
Expand Down
43 changes: 38 additions & 5 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,48 @@ func TestLoadConsumer(t *testing.T) {
assert.Equal(t, entities.Consumer{Id: "001", Username: "Jon Doe"}, resp)
}

/*
func TestGetConsumer(t *testing.T) {
assert.Equal(t, bridge.StepData{Method: "kong.client.get_consumer"}, getBack(func() { client.GetConsumer() }))
func TestAuthenticate(t *testing.T) {
var consumer *entities.Consumer = &entities.Consumer{Id: "001", Username: "Jon Doe"}
var credential *AuthenticatedCredential = &AuthenticatedCredential{Id: "000:00", ConsumerId: "001"}

c := mockClient(t, []bridgetest.MockStep{
{Method: "kong.client.authenticate",
Args: &kong_plugin_protocol.AuthenticateArgs{
Consumer: &kong_plugin_protocol.Consumer{
Id: consumer.Id,
CreatedAt: int64(consumer.CreatedAt),
Username: consumer.Username,
CustomId: consumer.CustomId,
Tags: consumer.Tags,
},
Credential: &kong_plugin_protocol.AuthenticatedCredential{
Id: credential.Id,
ConsumerId: credential.ConsumerId,
},
},
Ret: nil,
},
})

err := c.Authenticate(consumer, credential)

assert.NoError(t, err)
}

func TestAuthenticate(t *testing.T) {
func TestAuthenticateNil(t *testing.T) {
var consumer *entities.Consumer = nil
var credential *AuthenticatedCredential = nil
assert.Equal(t, bridge.StepData{Method: "kong.client.authenticate", Args: []interface{}{consumer, credential}}, getBack(func() { client.Authenticate(nil, nil) }))

c := mockClient(t, nil)

err := c.Authenticate(consumer, credential)

assert.EqualError(t, err, "either credential or consumer must be provided")
}

/*
func TestGetConsumer(t *testing.T) {
assert.Equal(t, bridge.StepData{Method: "kong.client.get_consumer"}, getBack(func() { client.GetConsumer() }))
}

func TestGetProtocol(t *testing.T) {
Expand Down
Loading