Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup #455

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add coverage
  • Loading branch information
shilgapira committed Aug 7, 2024
commit 624711e07e9145148e14dd8864fe9070047e5734
4 changes: 2 additions & 2 deletions descope/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,11 +1148,11 @@ func NewClient(conf ClientParams) *Client {
} else {
// App has set a different transport layer, we will not change its attributes, and use it as is
// this will include the tls config
rt = http.DefaultTransport
rt = http.DefaultTransport // notest
}
var timeout = time.Second * 60
if conf.RequestTimeout != 0 {
timeout = conf.RequestTimeout
timeout = conf.RequestTimeout // notest
}
httpClient = &http.Client{
Timeout: timeout,
Expand Down
2 changes: 1 addition & 1 deletion descope/internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (auth *authenticationService) ExchangeAccessKey(ctx context.Context, access
}

tokens, err := auth.extractTokens(jwtResponse)
if err != nil {
if err != nil { // notest
errMsg := err.Error()
if len(errMsg) == 0 {
errMsg = "Missing token in JWT response"
Expand Down
8 changes: 4 additions & 4 deletions descope/internal/mgmt/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type tenant struct {

func (t *tenant) Create(ctx context.Context, tenantRequest *descope.TenantRequest) (id string, err error) {
if tenantRequest == nil {
tenantRequest = &descope.TenantRequest{}
tenantRequest = &descope.TenantRequest{} // notest
}
return t.createWithID(ctx, "", tenantRequest)
}
Expand All @@ -24,7 +24,7 @@ func (t *tenant) CreateWithID(ctx context.Context, id string, tenantRequest *des
return utils.NewInvalidArgumentError("id")
}
if tenantRequest == nil {
tenantRequest = &descope.TenantRequest{}
tenantRequest = &descope.TenantRequest{} // notest
}
_, err := t.createWithID(ctx, id, tenantRequest)
return err
Expand Down Expand Up @@ -94,7 +94,7 @@ func (t *tenant) LoadAll(ctx context.Context) ([]*descope.Tenant, error) {
func (t *tenant) SearchAll(ctx context.Context, options *descope.TenantSearchOptions) ([]*descope.Tenant, error) {
// Init empty options if non given
if options == nil {
options = &descope.TenantSearchOptions{}
options = &descope.TenantSearchOptions{} // notest
}

req := makeSearchTenantRequest(options)
Expand All @@ -107,7 +107,7 @@ func (t *tenant) SearchAll(ctx context.Context, options *descope.TenantSearchOpt

func (t *tenant) GetSettings(ctx context.Context, tenantID string) (*descope.TenantSettings, error) {
if tenantID == "" {
return nil, utils.NewInvalidArgumentError("tenantID")
return nil, utils.NewInvalidArgumentError("tenantID") // notest
}
req := &api.HTTPRequest{
QueryParams: map[string]string{"id": tenantID},
Expand Down
4 changes: 2 additions & 2 deletions descope/internal/mgmt/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (u *user) CreateTestUser(ctx context.Context, loginID string, user *descope

func (u *user) CreateBatch(ctx context.Context, users []*descope.BatchUser) (*descope.UsersBatchResponse, error) {
if users == nil {
users = []*descope.BatchUser{}
users = []*descope.BatchUser{} // notest
}
return u.createBatch(ctx, users, nil)
}
Expand All @@ -64,7 +64,7 @@ func (u *user) Invite(ctx context.Context, loginID string, user *descope.UserReq

func (u *user) InviteBatch(ctx context.Context, users []*descope.BatchUser, options *descope.InviteOptions) (*descope.UsersBatchResponse, error) {
if users == nil {
users = []*descope.BatchUser{}
users = []*descope.BatchUser{} // notest
}
return u.createBatch(ctx, users, options)
}
Expand Down
80 changes: 77 additions & 3 deletions descope/internal/mgmt/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ func TestUserUpdatePhoneError(t *testing.T) {
require.Nil(t, res)
}

func TestUserUpdateNameSuccess(t *testing.T) {
func TestUserUpdateDisplayNameSuccess(t *testing.T) {
response := map[string]any{
"user": map[string]any{
"name": "foo",
Expand All @@ -942,20 +942,54 @@ func TestUserUpdateNameSuccess(t *testing.T) {
require.Equal(t, "foo", res.Name)
}

func TestUserUpdateNameBadInput(t *testing.T) {
func TestUserUpdateDisplayNameBadInput(t *testing.T) {
m := newTestMgmt(nil, helpers.DoOk(nil))
res, err := m.User().UpdateDisplayName(context.Background(), "", "foo")
require.Error(t, err)
require.Nil(t, res)
}

func TestUserUpdateNameError(t *testing.T) {
func TestUserUpdateDisplayNameError(t *testing.T) {
m := newTestMgmt(nil, helpers.DoBadRequest(nil))
res, err := m.User().UpdateDisplayName(context.Background(), "abc", "foo")
require.Error(t, err)
require.Nil(t, res)
}

func TestUserUpdateNamesSuccess(t *testing.T) {
response := map[string]any{
"user": map[string]any{
"name": "foo",
}}
m := newTestMgmt(nil, helpers.DoOkWithBody(func(r *http.Request) {
require.Equal(t, r.Header.Get("Authorization"), "Bearer a:key")
req := map[string]any{}
require.NoError(t, helpers.ReadBody(r, &req))
require.Equal(t, "abc", req["loginId"])
require.Equal(t, "g", req["givenName"])
require.Equal(t, "m", req["middleName"])
require.Equal(t, "f", req["familyName"])
}, response))
res, err := m.User().UpdateUserNames(context.Background(), "abc", "g", "m", "f")
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, "foo", res.Name)
}

func TestUserUpdateNamesBadInput(t *testing.T) {
m := newTestMgmt(nil, helpers.DoOk(nil))
res, err := m.User().UpdateUserNames(context.Background(), "", "g", "m", "f")
require.Error(t, err)
require.Nil(t, res)
}

func TestUserUpdateNamesError(t *testing.T) {
m := newTestMgmt(nil, helpers.DoBadRequest(nil))
res, err := m.User().UpdateUserNames(context.Background(), "abc", "g", "m", "f")
require.Error(t, err)
require.Nil(t, res)
}

func TestUserUpdatePictureSuccess(t *testing.T) {
response := map[string]any{
"user": map[string]any{
Expand Down Expand Up @@ -1288,6 +1322,46 @@ func TestUserRemoveTenantError(t *testing.T) {
require.Nil(t, res)
}

func TestUserSetTenantRoleSuccess(t *testing.T) {
response := map[string]any{
"user": map[string]any{
"userTenants": []map[string]any{
{
"tenantId": "123",
"roleNames": []string{"foo"},
},
},
}}
m := newTestMgmt(nil, helpers.DoOkWithBody(func(r *http.Request) {
require.Equal(t, r.Header.Get("Authorization"), "Bearer a:key")
req := map[string]any{}
require.NoError(t, helpers.ReadBody(r, &req))
require.Equal(t, "abc", req["loginId"])
require.Equal(t, "123", req["tenantId"])
require.Equal(t, []any{"foo"}, req["roleNames"])
}, response))
res, err := m.User().SetTenantRoles(context.Background(), "abc", "123", []string{"foo"})
require.NoError(t, err)
require.NotNil(t, res)
require.Len(t, res.UserTenants, 1)
require.Equal(t, "123", res.UserTenants[0].TenantID)
require.Equal(t, []string{"foo"}, res.UserTenants[0].Roles)
}

func TestUserSetTenantRoleBadInput(t *testing.T) {
m := newTestMgmt(nil, helpers.DoOk(nil))
res, err := m.User().SetTenantRoles(context.Background(), "", "123", []string{"foo"})
require.Error(t, err)
require.Nil(t, res)
}

func TestUserSetTenantRoleError(t *testing.T) {
m := newTestMgmt(nil, helpers.DoBadRequest(nil))
res, err := m.User().SetTenantRoles(context.Background(), "abc", "123", []string{"foo"})
require.Error(t, err)
require.Nil(t, res)
}

func TestUserAddTenantRoleSuccess(t *testing.T) {
response := map[string]any{
"user": map[string]any{
Expand Down