Add tests for UpateClient API

This commit is contained in:
Cosmin Cojocar 2018-11-12 18:43:48 +01:00
parent 6531b256e2
commit c9b18b2785

View file

@ -290,3 +290,174 @@ func TestRefreshToken(t *testing.T) {
t.Fatalf("Refresh token returned inspite of revoking it.")
}
}
func TestUpdateClient(t *testing.T) {
logger := &logrus.Logger{
Out: os.Stderr,
Formatter: &logrus.TextFormatter{DisableColors: true},
Level: logrus.DebugLevel,
}
s := memory.New(logger)
client := newAPI(s, logger, t)
defer client.Close()
ctx := context.Background()
createClient := func(t *testing.T, clientId string) {
resp, err := client.CreateClient(ctx, &api.CreateClientReq{
Client: &api.Client{
Id: clientId,
Secret: "",
RedirectUris: []string{},
TrustedPeers: nil,
Public: true,
Name: "",
LogoUrl: "",
},
})
if err != nil {
t.Fatalf("unable to create the client: %v", err)
}
if resp == nil {
t.Fatalf("create client returned no response")
}
if resp.AlreadyExists {
t.Error("existing client was found")
}
if resp.Client == nil {
t.Fatalf("no client created")
}
}
deleteClient := func(t *testing.T, clientId string) {
resp, err := client.DeleteClient(ctx, &api.DeleteClientReq{
Id: clientId,
})
if err != nil {
t.Fatalf("unable to delete the client: %v", err)
}
if resp == nil {
t.Fatalf("delete client delete client returned no response")
}
}
tests := map[string]struct {
setup func(t *testing.T, clientId string)
cleanup func(t *testing.T, clientId string)
req *api.UpdateClientReq
wantErr bool
want *api.UpdateClientResp
}{
"update client": {
setup: createClient,
cleanup: deleteClient,
req: &api.UpdateClientReq{
Id: "test",
RedirectUris: []string{"https://redirect"},
TrustedPeers: []string{"test"},
Public: true,
Name: "test",
LogoUrl: "https://logout",
},
wantErr: false,
want: &api.UpdateClientResp{
NotFound: false,
},
},
"update client without ID": {
setup: createClient,
cleanup: deleteClient,
req: &api.UpdateClientReq{
Id: "",
RedirectUris: nil,
TrustedPeers: nil,
Public: false,
Name: "test",
LogoUrl: "test",
},
wantErr: true,
want: &api.UpdateClientResp{
NotFound: false,
},
},
"update client which not exists ": {
req: &api.UpdateClientReq{
Id: "test",
RedirectUris: nil,
TrustedPeers: nil,
Public: false,
Name: "test",
LogoUrl: "test",
},
wantErr: true,
want: &api.UpdateClientResp{
NotFound: false,
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
if tc.setup != nil {
tc.setup(t, tc.req.Id)
}
resp, err := client.UpdateClient(ctx, tc.req)
if err != nil && !tc.wantErr {
t.Fatalf("failed to update the client: %v", err)
}
if !tc.wantErr {
if resp == nil {
t.Fatalf("update client response not found")
}
if tc.want.NotFound != resp.NotFound {
t.Errorf("expected in response NotFound: %t", tc.want.NotFound)
}
client, err := s.GetClient(tc.req.Id)
if err != nil {
t.Errorf("no client found in the storage: %v", err)
}
if tc.req.Id != client.ID {
t.Errorf("expected stored client with ID: %s, found %s", tc.req.Id, client.ID)
}
if tc.req.Name != client.Name {
t.Errorf("expected stored client with Name: %s, found %s", tc.req.Name, client.Name)
}
if tc.req.LogoUrl != client.LogoURL {
t.Errorf("expected stored client with LogoURL: %s, found %s", tc.req.LogoUrl, client.LogoURL)
}
for _, redirectURI := range tc.req.RedirectUris {
found := find(redirectURI, client.RedirectURIs)
if !found {
t.Errorf("expected redirect URI: %s", redirectURI)
}
}
for _, peer := range tc.req.TrustedPeers {
found := find(peer, client.TrustedPeers)
if !found {
t.Errorf("expected trusted peer: %s", peer)
}
}
}
if tc.cleanup != nil {
tc.cleanup(t, tc.req.Id)
}
})
}
}
func find(item string, items []string) bool {
for _, i := range items {
if item == i {
return true
}
}
return false
}