Skip to content

Commit

Permalink
⬆️ client: added published to enable filtering clients by published…
Browse files Browse the repository at this point in the history
… state.
  • Loading branch information
matthewhartstonge committed Sep 25, 2019
1 parent 37e3426 commit d8c0344
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ type Client struct {
// Contacts contains a list ways to contact the developers responsible for
// this OAuth 2.0 client, typically email addresses.
Contacts []string `bson:"contacts" json:"contacts" xml:"contacts"`

// Published provides a switch to hide specific clients if not quite ready
// for the prime time, or if wanting to keep them hidden.
Published bool `bson:"published" json:"published" xml:"published"`
}

// GetID returns the client's Client ID.
Expand Down Expand Up @@ -325,6 +329,10 @@ func (c Client) Equal(x Client) bool {
return false
}

if c.Published != x.Published {
return false
}

return true
}

Expand Down
2 changes: 2 additions & 0 deletions client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ type ListClientsRequest struct {
Public bool `json:"public" xml:"public"`
// Disabled filters clients based on denied access.
Disabled bool `json:"disabled" xml:"disabled"`
// Published filters clients based on published status.
Published bool `json:"published" xml:"published"`
}
3 changes: 3 additions & 0 deletions mongo/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ func (c *ClientManager) List(ctx context.Context, filter storage.ListClientsRequ
if filter.Disabled {
query["disabled"] = filter.Disabled
}
if filter.Published {
query["published"] = filter.Published
}

// Trace how long the Mongo operation takes to complete.
span, _ := traceMongoCall(ctx, dbTrace{
Expand Down
38 changes: 38 additions & 0 deletions mongo/client_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ func TestClientManager_List(t *testing.T) {
// generate our expected data.
expected := createClient(t, ctx, store)

publishedClient := storage.Client{
ID: uuid.New(),
CreateTime: time.Now().Unix(),
UpdateTime: time.Now().Unix() + 600,
AllowedAudiences: []string{},
AllowedRegions: []string{},
AllowedTenantAccess: []string{},
GrantTypes: []string{},
ResponseTypes: []string{},
Scopes: []string{},
Name: "published client",
RedirectURIs: []string{},
Contacts: []string{},
Published: true,
}
publishedClient = createNewClient(t, ctx, store, publishedClient)

type args struct {
filter storage.ListClientsRequest
}
Expand Down Expand Up @@ -351,6 +368,19 @@ func TestClientManager_List(t *testing.T) {
wantErr: false,
err: nil,
},
{
name: "should filter for published clients",
args: args{
filter: storage.ListClientsRequest{
Published: true,
},
},
wantResults: []storage.Client{
publishedClient,
},
wantErr: false,
err: nil,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -413,11 +443,16 @@ func expectedClient() storage.Client {
Contacts: []string{
"John Doe <j.doe@example.com>",
},
Published: false,
}
}

func createClient(t *testing.T, ctx context.Context, store *mongo.Store) storage.Client {
expected := expectedClient()
return createNewClient(t, ctx, store, expected)
}

func createNewClient(t *testing.T, ctx context.Context, store *mongo.Store, expected storage.Client) storage.Client {
got, err := store.ClientManager.Create(ctx, expected)
if err != nil {
AssertError(t, err, nil, "create should return no database errors")
Expand All @@ -429,6 +464,9 @@ func createClient(t *testing.T, ctx context.Context, store *mongo.Store) storage
t.FailNow()
}

expected.ID = got.ID
expected.CreateTime = got.CreateTime
expected.UpdateTime = got.UpdateTime
expected.Secret = got.Secret
if !reflect.DeepEqual(got, expected) {
AssertError(t, got, expected, "client not equal")
Expand Down

0 comments on commit d8c0344

Please sign in to comment.