Skip to content

Commit

Permalink
including service and client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcreado committed May 26, 2022
1 parent b142707 commit fee3ae9
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 1 deletion.
133 changes: 133 additions & 0 deletions testing/client_management_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package testing

import (
"strconv"
"testing"

authlete "github.com/authlete/openapi-for-go"
)

func TestClientLifecycle(t *testing.T) {

authleteClient, srv, apiCtx, newCli := createTestClient(t)
apiKey := strconv.FormatInt(srv.GetApiKey(), 10)
defer ensureDeleteService(apiKey, authleteClient)

client_id := strconv.FormatInt(newCli.GetClientId(), 10)
client_secret := newCli.GetClientSecret()

// update the client

att1 := authlete.NewPair()
att1.SetKey("key1")
att1.SetValue("val1")
newCli.SetAttributes([]authlete.Pair{*att1})
newCli.SetDescription("Changed Description")

updCli, _, errUp := authleteClient.ClientManagementApi.ClientUpdateApi(apiCtx, client_id).Client(*newCli).Execute()

if errUp != nil {
t.Errorf("An error occured when updating a testing client: %q", errUp.Error())
}

if len(updCli.GetAttributes()) == 0 {
t.Errorf("Changes on attributes not reflected on returned client from update client api")
}

// get the client

getCli, _, errGet := authleteClient.ClientManagementApi.ClientGetApi(apiCtx, client_id).Execute()

if errGet != nil {
t.Errorf("An error occured when retrieving the testing client: %q", errGet.Error())
}

if updCli.GetDescription() != getCli.GetDescription() {
t.Errorf("The change to description is not reflected on retrieved client ")
}

// list the clients

cliList, _, errList := authleteClient.ClientManagementApi.ClientGetListApi(apiCtx).Start(0).End(100).Execute()

if errList != nil {
t.Errorf("An error occured when retrieving the client list: %q", errList.Error())
}

found := false
var cli authlete.Client
for _, cli = range cliList.GetClients() {
if cli.GetClientId() == newCli.GetClientId() {
found = true
break
}
}
if !found {
t.Errorf("Could not find the created client on client list")
}
if cli.GetClientSecret() != client_secret {
t.Errorf("The client secret was changed on previous calls")
}

// delete the client

_, errDelete := authleteClient.ClientManagementApi.ClientDeleteApi(apiCtx, client_id).Execute()

if errDelete != nil {
t.Errorf("An error occured when deleting the test client: %q", errDelete.Error())
}

// check if it was deleted

cliList, _, errList = authleteClient.ClientManagementApi.ClientGetListApi(apiCtx).Start(0).End(100).Execute()

if errList != nil {
t.Errorf("An error occured when retrieving the client list: %q", errList.Error())
}

found = false
for _, cli = range cliList.GetClients() {
if cli.GetClientId() == newCli.GetClientId() {
found = true
break
}
}
if found {
t.Errorf("deleted client still on client list")
}
}

func TestSecretManagement(t *testing.T) {
authleteClient, srv, apiCtx, newCli := createTestClient(t)
apiKey := strconv.FormatInt(srv.GetApiKey(), 10)
defer ensureDeleteService(apiKey, authleteClient)

client_id := strconv.FormatInt(newCli.GetClientId(), 10)

client_secret_up := "jsadfkljhfadkjsadfj1"
req := authlete.NewClientSecretUpdateRequest(client_secret_up)
resp, _, err := authleteClient.ClientManagementApi.ClientSecretUpdateApi(apiCtx, client_id).ClientSecretUpdateRequest(*req).Execute()

if err != nil {
t.Errorf("An error occured when updating the secret: %q", err.Error())
}

if resp.GetNewClientSecret() != client_secret_up {
t.Errorf("new secret not matching %q - expected %q", resp.GetNewClientSecret(), client_secret_up)
}

respRefresh, _, errRefresh := authleteClient.ClientManagementApi.ClientSecretRefreshApi(apiCtx, client_id).Execute()

if errRefresh != nil {
t.Errorf("An error occured when updating the secret: %q", errRefresh.Error())
}

if respRefresh.GetOldClientSecret() != client_secret_up {
t.Errorf("old secret not matching %q - expected %q", respRefresh.GetOldClientSecret(), client_secret_up)
}

if respRefresh.GetOldClientSecret() == respRefresh.GetNewClientSecret() {
t.Errorf("old secret and new secret are matching %q", respRefresh.GetOldClientSecret())
}

}
2 changes: 1 addition & 1 deletion testing/service_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestServiceLifecycle(t *testing.T) {

getSrv, _, errGet := authleteClient.ServiceManagementApi.ServiceGetApi(auth, key).Execute()
if errGet != nil {
t.Errorf("An error occured when create a service: %q", err.Error())
t.Errorf("An error occured when create a service: %q", errGet.Error())
}

if getSrv.GetDescription() != "Changed Description" {
Expand Down
48 changes: 48 additions & 0 deletions testing/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package testing
import (
"context"
"os"
"strconv"
"testing"

authlete "github.com/authlete/openapi-for-go"
)
Expand All @@ -29,6 +31,14 @@ func createSOContext() context.Context {
})
}

func createAPIContext(apiKey string, apiSecret string) context.Context {

return context.WithValue(context.Background(), authlete.ContextBasicAuth, authlete.BasicAuth{
UserName: apiKey,
Password: apiSecret,
})
}

func ensureDeleteService(apiKey string, cli *authlete.APIClient) {

ctx := createSOContext()
Expand Down Expand Up @@ -64,3 +74,41 @@ func authorizationCodeDTO() *authlete.Service {
}
return testService
}

func createTestClient(t *testing.T) (*authlete.APIClient, *authlete.Service, context.Context, *authlete.Client) {
authleteClient := createClient()
testservice := authorizationCodeDTO()
auth := createSOContext()
srv, _, err := authleteClient.ServiceManagementApi.ServiceCreateApi(auth).Service(*testservice).Execute()

if err != nil {
t.Errorf("An error occured when create a testing service: %q", err.Error())
}

apiKey := strconv.FormatInt(srv.GetApiKey(), 10)
apiSecret := srv.GetApiSecret()

oauthClient := authlete.NewClient()
oauthClient.SetClientIdAlias("oauthclient1")
oauthClient.SetDescription("this is a oauthclient")
oauthClient.SetApplicationType(authlete.APPLICATIONTYPE_WEB)
oauthClient.SetClientName("oauthclient")
oauthClient.SetDeveloper("gotest")
oauthClient.SetResponseTypes([]authlete.ResponseType{authlete.RESPONSETYPE_CODE})
oauthClient.SetGrantTypes([]authlete.GrantType{authlete.GRANTTYPE_AUTHORIZATION_CODE, authlete.GRANTTYPE_REFRESH_TOKEN})

apiCtx := createAPIContext(apiKey, apiSecret)

newCli, _, errCli := authleteClient.ClientManagementApi.ClientCreateApi(apiCtx).Client(*oauthClient).Execute()

if errCli != nil {
defer ensureDeleteService(apiKey, authleteClient)
t.Errorf("An error occured when create a testing client: %q", errCli.Error())
}

if newCli.GetClientId() == 0 || newCli.GetClientSecret() == "" {
defer ensureDeleteService(apiKey, authleteClient)
t.Errorf("Testing client has no key or secret")
}
return authleteClient, srv, apiCtx, newCli
}

0 comments on commit fee3ae9

Please sign in to comment.