-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kyoshiro.maruo
committed
Mar 16, 2021
1 parent
33f8000
commit edb470d
Showing
5 changed files
with
175 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package impl | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_addCmd(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
users []string | ||
groups []string | ||
|
||
valid bool | ||
}{ | ||
{name: "it successes", users: []string{"user1@email.com"}, groups: []string{"my-group-3", "my-group-4"}, valid: true}, | ||
{name: "it fails when the user is not found", users: []string{"user2@email.com"}, groups: []string{"my-group-3"}, valid: false}, | ||
{name: "it fails when the group is not found", users: []string{"user1@email.com"}, groups: []string{"my-group-5"}, valid: false}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
err := addCmd(&mockClient{respSearchUser: []byte(respSearchUserJSON), respGetGroups: []byte(respGetGroupJSON)}, c.users, c.groups) | ||
if c.valid { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.Error(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
var respSearchUserJSON = ` | ||
{ | ||
"count": 1, | ||
"page": 1, | ||
"page_size": 20, | ||
"results": [ | ||
{ | ||
"active_at": "2021-03-16T11:12:58Z", | ||
"auth_type": "external", | ||
"created_at": "2018-08-07T01:13:40.265Z", | ||
"disabled_at": null, | ||
"email": "user1@email.com", | ||
"groups": [ | ||
{ | ||
"id": 33, | ||
"name": "my-group-33" | ||
} | ||
], | ||
"id": 1, | ||
"is_disabled": false, | ||
"is_email_verified": true, | ||
"is_invitation_pending": false, | ||
"name": "snowhork", | ||
"profile_image_url": "example.com", | ||
"updated_at": "2021-03-16T11:13:03.856Z" | ||
} | ||
] | ||
} | ||
` | ||
|
||
var respGetGroupJSON = ` | ||
[ | ||
{ | ||
"created_at": "2016-05-22T09:30:53.770Z", | ||
"id": 1, | ||
"name": "admin", | ||
"permissions": [ | ||
"admin", | ||
"super_admin" | ||
], | ||
"type": "builtin" | ||
}, | ||
{ | ||
"created_at": "2016-05-22T09:30:53.772Z", | ||
"id": 2, | ||
"name": "default", | ||
"permissions": [], | ||
"type": "builtin" | ||
}, | ||
{ | ||
"created_at": "2016-08-22T06:37:52.138Z", | ||
"id": 3, | ||
"name": "my-group-3", | ||
"permissions": [ | ||
"create_dashboard", | ||
"create_query", | ||
"edit_dashboard", | ||
"edit_query", | ||
"view_query", | ||
"view_source", | ||
"execute_query", | ||
"list_users", | ||
"schedule_query", | ||
"list_dashboards", | ||
"list_alerts", | ||
"list_data_sources" | ||
], | ||
"type": "regular" | ||
}, | ||
{ | ||
"created_at": "2016-08-22T06:37:52.138Z", | ||
"id": 4, | ||
"name": "my-group-4", | ||
"permissions": [ | ||
"create_dashboard", | ||
"create_query", | ||
"edit_dashboard", | ||
"edit_query", | ||
"view_query", | ||
"view_source", | ||
"execute_query", | ||
"list_users", | ||
"schedule_query", | ||
"list_dashboards", | ||
"list_alerts", | ||
"list_data_sources" | ||
], | ||
"type": "regular" | ||
} | ||
] | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package impl | ||
|
||
type mockClient struct { | ||
respSearchUser, | ||
respGetGroups, | ||
respGetQuery, | ||
respDataSource, | ||
respDashboard, | ||
respAddMember []byte | ||
} | ||
|
||
func (c *mockClient) SearchUser(q string) ([]byte, error) { | ||
return c.respSearchUser, nil | ||
} | ||
|
||
func (c *mockClient) GetGroups() ([]byte, error) { | ||
return c.respGetGroups, nil | ||
} | ||
|
||
func (c *mockClient) GetQuery(id int) ([]byte, error) { | ||
return c.respGetQuery, nil | ||
} | ||
|
||
func (c *mockClient) GetDataSource(id int) ([]byte, error) { | ||
return c.respDataSource, nil | ||
} | ||
|
||
func (c *mockClient) GetDashboard(id string) ([]byte, error) { | ||
return c.respDashboard, nil | ||
} | ||
|
||
func (c *mockClient) AddMember(groupID, userID int) ([]byte, error) { | ||
return c.respAddMember, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters