feat(oauth2): confidential_client handling (#625)
This PR adds support for the `confidential_client` in `oauth2` to reflect the swagger APIs. It has been surfaced here: https://github.com/Lerentis/terraform-provider-gitea/pull/46 Simple tests have also been added. Please note that in this PR I am considering the current behaviour: > if confidential_client is not set, it's assumed that it's false However, from the swagger, it seems that the implicit default is `true` instead. Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/625 Co-authored-by: Alessandro De Blasis <alex@deblasis.net> Co-committed-by: Alessandro De Blasis <alex@deblasis.net>
This commit is contained in:
parent
315cf7aac8
commit
f4be505bf6
2 changed files with 62 additions and 26 deletions
|
@ -18,6 +18,7 @@ type Oauth2 struct {
|
||||||
ClientID string `json:"client_id"`
|
ClientID string `json:"client_id"`
|
||||||
ClientSecret string `json:"client_secret"`
|
ClientSecret string `json:"client_secret"`
|
||||||
RedirectURIs []string `json:"redirect_uris"`
|
RedirectURIs []string `json:"redirect_uris"`
|
||||||
|
ConfidentialClient bool `json:"confidential_client"`
|
||||||
Created time.Time `json:"created"`
|
Created time.Time `json:"created"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,8 +30,8 @@ type ListOauth2Option struct {
|
||||||
// CreateOauth2Option required options for creating an Application
|
// CreateOauth2Option required options for creating an Application
|
||||||
type CreateOauth2Option struct {
|
type CreateOauth2Option struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
RedirectURIs []string `json:"redirect_uris"`
|
|
||||||
ConfidentialClient bool `json:"confidential_client"`
|
ConfidentialClient bool `json:"confidential_client"`
|
||||||
|
RedirectURIs []string `json:"redirect_uris"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateOauth2 create an Oauth2 Application and returns a completed Oauth2 object.
|
// CreateOauth2 create an Oauth2 Application and returns a completed Oauth2 object.
|
||||||
|
|
|
@ -18,27 +18,62 @@ func TestOauth2(t *testing.T) {
|
||||||
user := createTestUser(t, "oauth2_user", c)
|
user := createTestUser(t, "oauth2_user", c)
|
||||||
c.SetSudo(user.UserName)
|
c.SetSudo(user.UserName)
|
||||||
|
|
||||||
newApp, _, err := c.CreateOauth2(CreateOauth2Option{Name: "test", RedirectURIs: []string{"http://test/test"}})
|
type test struct {
|
||||||
assert.NoError(t, err)
|
name string
|
||||||
assert.NotNil(t, newApp)
|
confidentialClient *bool
|
||||||
assert.EqualValues(t, "test", newApp.Name)
|
}
|
||||||
|
boolTrue := true
|
||||||
|
boolFalse := false
|
||||||
|
|
||||||
|
testCases := []test{
|
||||||
|
{"ConfidentialClient unset should fallback to false", nil},
|
||||||
|
{"ConfidentialClient true", &boolTrue},
|
||||||
|
{"ConfidentialClient false", &boolFalse},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
createOptions := CreateOauth2Option{
|
||||||
|
Name: "test",
|
||||||
|
RedirectURIs: []string{"http://test/test"},
|
||||||
|
}
|
||||||
|
if testCase.confidentialClient != nil {
|
||||||
|
createOptions.ConfidentialClient = *testCase.confidentialClient
|
||||||
|
}
|
||||||
|
|
||||||
|
newApp, _, err := c.CreateOauth2(createOptions)
|
||||||
|
assert.NoError(t, err, testCase.name)
|
||||||
|
assert.NotNil(t, newApp, testCase.name)
|
||||||
|
assert.EqualValues(t, "test", newApp.Name, testCase.name)
|
||||||
|
if testCase.confidentialClient != nil {
|
||||||
|
assert.EqualValues(t, *testCase.confidentialClient, newApp.ConfidentialClient, testCase.name)
|
||||||
|
} else {
|
||||||
|
assert.EqualValues(t, false, newApp.ConfidentialClient, testCase.name)
|
||||||
|
}
|
||||||
|
|
||||||
a, _, err := c.ListOauth2(ListOauth2Option{})
|
a, _, err := c.ListOauth2(ListOauth2Option{})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err, testCase.name)
|
||||||
assert.Len(t, a, 1)
|
assert.Len(t, a, 1, testCase.name)
|
||||||
assert.EqualValues(t, newApp.Name, a[0].Name)
|
assert.EqualValues(t, newApp.Name, a[0].Name, testCase.name)
|
||||||
|
assert.EqualValues(t, newApp.ConfidentialClient, a[0].ConfidentialClient, testCase.name)
|
||||||
|
|
||||||
b, _, err := c.GetOauth2(newApp.ID)
|
b, _, err := c.GetOauth2(newApp.ID)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err, testCase.name)
|
||||||
assert.EqualValues(t, newApp.Name, b.Name)
|
assert.EqualValues(t, newApp.Name, b.Name, testCase.name)
|
||||||
|
assert.EqualValues(t, newApp.ConfidentialClient, b.ConfidentialClient, testCase.name)
|
||||||
|
|
||||||
b, _, err = c.UpdateOauth2(newApp.ID, CreateOauth2Option{Name: newApp.Name, RedirectURIs: []string{"https://test/login"}})
|
b, _, err = c.UpdateOauth2(newApp.ID, CreateOauth2Option{
|
||||||
assert.NoError(t, err)
|
Name: newApp.Name,
|
||||||
assert.EqualValues(t, newApp.Name, b.Name)
|
ConfidentialClient: !newApp.ConfidentialClient,
|
||||||
assert.EqualValues(t, "https://test/login", b.RedirectURIs[0])
|
RedirectURIs: []string{"https://test/login"},
|
||||||
assert.EqualValues(t, newApp.ID, b.ID)
|
})
|
||||||
assert.NotEqual(t, newApp.ClientSecret, b.ClientSecret)
|
assert.NoError(t, err, testCase.name)
|
||||||
|
assert.EqualValues(t, newApp.Name, b.Name, testCase.name)
|
||||||
|
assert.EqualValues(t, "https://test/login", b.RedirectURIs[0], testCase.name)
|
||||||
|
assert.EqualValues(t, newApp.ID, b.ID, testCase.name)
|
||||||
|
assert.NotEqual(t, newApp.ClientSecret, b.ClientSecret, testCase.name)
|
||||||
|
assert.NotEqual(t, newApp.ConfidentialClient, b.ConfidentialClient, testCase.name)
|
||||||
|
|
||||||
_, err = c.DeleteOauth2(newApp.ID)
|
_, err = c.DeleteOauth2(newApp.ID)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err, testCase.name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue