Skip to content

Commit 271cc2c

Browse files
lvermeiregszr
authored andcommitted
fix(client): allow nil values authenticate
Either `consumer` or `credential` may be nil.
1 parent 6735f30 commit 271cc2c

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

client/client.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,27 @@ func (c Client) GetConsumer() (consumer entities.Consumer, err error) {
127127
// for the current request. While both consumer and credential can be nil,
128128
// it is required that at least one of them exists. Otherwise this function will throw an error.
129129
func (c Client) Authenticate(consumer *entities.Consumer, credential *AuthenticatedCredential) error {
130-
if consumer == nil {
131-
return fmt.Errorf("Invalid consumer")
130+
if consumer == nil && credential == nil {
131+
return fmt.Errorf("either credential or consumer must be provided")
132132
}
133-
if credential == nil {
134-
return fmt.Errorf("Invalid credential")
135-
}
136-
arg := &kong_plugin_protocol.AuthenticateArgs{
137-
Consumer: &kong_plugin_protocol.Consumer{
133+
134+
arg := &kong_plugin_protocol.AuthenticateArgs{}
135+
if consumer != nil {
136+
arg.Consumer = &kong_plugin_protocol.Consumer{
138137
Id: consumer.Id,
139138
CreatedAt: int64(consumer.CreatedAt),
140139
Username: consumer.Username,
141140
CustomId: consumer.CustomId,
142141
Tags: consumer.Tags,
143-
},
144-
Credential: &kong_plugin_protocol.AuthenticatedCredential{
142+
}
143+
}
144+
if credential != nil {
145+
arg.Credential = &kong_plugin_protocol.AuthenticatedCredential{
145146
Id: credential.Id,
146147
ConsumerId: credential.ConsumerId,
147-
},
148+
}
148149
}
150+
149151
err := c.Ask(`kong.client.authenticate`, arg, nil)
150152
return err
151153
}

client/client_test.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,48 @@ func TestLoadConsumer(t *testing.T) {
7676
assert.Equal(t, entities.Consumer{Id: "001", Username: "Jon Doe"}, resp)
7777
}
7878

79-
/*
80-
func TestGetConsumer(t *testing.T) {
81-
assert.Equal(t, bridge.StepData{Method: "kong.client.get_consumer"}, getBack(func() { client.GetConsumer() }))
79+
func TestAuthenticate(t *testing.T) {
80+
var consumer *entities.Consumer = &entities.Consumer{Id: "001", Username: "Jon Doe"}
81+
var credential *AuthenticatedCredential = &AuthenticatedCredential{Id: "000:00", ConsumerId: "001"}
82+
83+
c := mockClient(t, []bridgetest.MockStep{
84+
{Method: "kong.client.authenticate",
85+
Args: &kong_plugin_protocol.AuthenticateArgs{
86+
Consumer: &kong_plugin_protocol.Consumer{
87+
Id: consumer.Id,
88+
CreatedAt: int64(consumer.CreatedAt),
89+
Username: consumer.Username,
90+
CustomId: consumer.CustomId,
91+
Tags: consumer.Tags,
92+
},
93+
Credential: &kong_plugin_protocol.AuthenticatedCredential{
94+
Id: credential.Id,
95+
ConsumerId: credential.ConsumerId,
96+
},
97+
},
98+
Ret: nil,
99+
},
100+
})
101+
102+
err := c.Authenticate(consumer, credential)
103+
104+
assert.NoError(t, err)
82105
}
83106

84-
func TestAuthenticate(t *testing.T) {
107+
func TestAuthenticateNil(t *testing.T) {
85108
var consumer *entities.Consumer = nil
86109
var credential *AuthenticatedCredential = nil
87-
assert.Equal(t, bridge.StepData{Method: "kong.client.authenticate", Args: []interface{}{consumer, credential}}, getBack(func() { client.Authenticate(nil, nil) }))
110+
111+
c := mockClient(t, nil)
112+
113+
err := c.Authenticate(consumer, credential)
114+
115+
assert.EqualError(t, err, "either credential or consumer must be provided")
116+
}
117+
118+
/*
119+
func TestGetConsumer(t *testing.T) {
120+
assert.Equal(t, bridge.StepData{Method: "kong.client.get_consumer"}, getBack(func() { client.GetConsumer() }))
88121
}
89122
90123
func TestGetProtocol(t *testing.T) {

0 commit comments

Comments
 (0)