Adding GetUsersReponse and modifying Users structure from Email to EmailAddress

This commit is contained in:
gfleury 2020-03-17 13:57:44 -03:00
parent 672adbf0aa
commit 1864c1339e
2 changed files with 156 additions and 60 deletions

View File

@ -79,24 +79,29 @@ type UserWithNameEmail struct {
} }
type UserWithLinks struct { type UserWithLinks struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Email string `json:"emailAddress,omitempty"` EmailAddress string `json:"emailAddress,omitempty"`
ID int `json:"id,omitempty"` ID int `json:"id,omitempty"`
DisplayName string `json:"displayName,omitempty"` DisplayName string `json:"displayName,omitempty"`
Active bool `json:"active,omitempty"` Active bool `json:"active,omitempty"`
Slug string `json:"slug,omitempty"` Slug string `json:"slug,omitempty"`
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
Links Links `json:"links,omitempty"` Links Links `json:"links,omitempty"`
} }
type User struct { type User struct {
Name string `json:"name"` Name string `json:"name"`
Email string `json:"emailAddress"` EmailAddress string `json:"emailAddress"`
ID int `json:"id"` ID int `json:"id"`
DisplayName string `json:"displayName"` DisplayName string `json:"displayName"`
Active bool `json:"active"` Active bool `json:"active"`
Slug string `json:"slug"` Slug string `json:"slug"`
Type string `json:"type"` Type string `json:"type"`
DirectoryName string `json:"directoryName"`
Deletable bool `json:"deletable"`
LastAuthenticationTimestamp int64 `json:"lastAuthenticationTimestamp"`
MutableDetails bool `json:"mutableDetails"`
MutableGroups bool `json:"mutableGroups"`
} }
type UserWithMetadata struct { type UserWithMetadata struct {
@ -464,6 +469,13 @@ func GetContentResponse(r *APIResponse) (Content, error) {
return c, err return c, err
} }
// GetUsersResponse casts users into structure
func GetUsersResponse(r *APIResponse) ([]User, error) {
var c []User
err := mapstructure.Decode(r.Values["values"], &c)
return c, err
}
// GetUsersPermissionResponse casts user permissions into structure // GetUsersPermissionResponse casts user permissions into structure
func GetUsersPermissionResponse(r *APIResponse) ([]UserPermission, error) { func GetUsersPermissionResponse(r *APIResponse) ([]UserPermission, error) {
var c []UserPermission var c []UserPermission

View File

@ -430,13 +430,13 @@ func TestGetPullRequestResponse(t *testing.T) {
Locked: false, Locked: false,
Author: &UserWithMetadata{ Author: &UserWithMetadata{
User: UserWithLinks{ User: UserWithLinks{
Name: "tom", Name: "tom",
// Email: "tom@example.com", EmailAddress: "tom@example.com",
ID: 115026, ID: 115026,
DisplayName: "Tom", DisplayName: "Tom",
Active: true, Active: true,
Slug: "tom", Slug: "tom",
Type: "NORMAL", Type: "NORMAL",
}, },
Role: "AUTHOR", Role: "AUTHOR",
Approved: true, Approved: true,
@ -445,13 +445,13 @@ func TestGetPullRequestResponse(t *testing.T) {
Reviewers: []UserWithMetadata{ Reviewers: []UserWithMetadata{
{ {
User: UserWithLinks{ User: UserWithLinks{
Name: "jcitizen", Name: "jcitizen",
// Email: "jane@example.com", EmailAddress: "jane@example.com",
ID: 101, ID: 101,
DisplayName: "Jane Citizen", DisplayName: "Jane Citizen",
Active: true, Active: true,
Slug: "jcitizen", Slug: "jcitizen",
Type: "NORMAL", Type: "NORMAL",
}, },
Role: "REVIEWER", Role: "REVIEWER",
Approved: true, Approved: true,
@ -461,13 +461,13 @@ func TestGetPullRequestResponse(t *testing.T) {
Participants: []UserWithMetadata{ Participants: []UserWithMetadata{
{ {
User: UserWithLinks{ User: UserWithLinks{
Name: "dick", Name: "dick",
// Email: "dick@example.com", EmailAddress: "dick@example.com",
ID: 3083181, ID: 3083181,
DisplayName: "Dick", DisplayName: "Dick",
Active: true, Active: true,
Slug: "dick", Slug: "dick",
Type: "NORMAL", Type: "NORMAL",
}, },
Role: "PARTICIPANT", Role: "PARTICIPANT",
Approved: false, Approved: false,
@ -475,13 +475,13 @@ func TestGetPullRequestResponse(t *testing.T) {
}, },
{ {
User: UserWithLinks{ User: UserWithLinks{
Name: "harry", Name: "harry",
// Email: "harry@example.com", EmailAddress: "harry@example.com",
ID: 99049120, ID: 99049120,
DisplayName: "Harry", DisplayName: "Harry",
Active: true, Active: true,
Slug: "harry", Slug: "harry",
Type: "NORMAL", Type: "NORMAL",
}, },
Role: "PARTICIPANT", Role: "PARTICIPANT",
Approved: true, Approved: true,
@ -687,16 +687,13 @@ func TestGetUsersPermissionResponse(t *testing.T) {
Values: map[string]interface{}{ Values: map[string]interface{}{
"values": []interface{}{map[string]interface{}{ "values": []interface{}{map[string]interface{}{
"user": map[string]interface{}{ "user": map[string]interface{}{
"name": "jcitizen", "name": "jcitizen",
// TODO: This field should be emailAddress according to the REST API "emailAddress": "jane@example.com",
// documentation, but is defined as Email in the User struct. Mapstruct # "id": 101,
// therefore only decodes this when reffered to as 'email', which is plain wrong. "displayName": "Jane Citizen",
// "email": "jane@example.com", "active": true,
"id": 101, "slug": "jcitizen",
"displayName": "Jane Citizen", "type": "NORMAL",
"active": true,
"slug": "jcitizen",
"type": "NORMAL",
}, },
"permission": "REPO_ADMIN", "permission": "REPO_ADMIN",
}}, }},
@ -706,13 +703,13 @@ func TestGetUsersPermissionResponse(t *testing.T) {
want: []UserPermission{ want: []UserPermission{
UserPermission{ UserPermission{
User: User{ User: User{
Name: "jcitizen", Name: "jcitizen",
// Email: "jane@example.com", EmailAddress: "jane@example.com",
ID: 101, ID: 101,
DisplayName: "Jane Citizen", DisplayName: "Jane Citizen",
Active: true, Active: true,
Slug: "jcitizen", Slug: "jcitizen",
Type: "NORMAL", Type: "NORMAL",
}, },
Permission: PermissionRepositoryAdmin.String(), Permission: PermissionRepositoryAdmin.String(),
}, },
@ -734,6 +731,93 @@ func TestGetUsersPermissionResponse(t *testing.T) {
} }
} }
func TestGetUsersResponse(t *testing.T) {
type args struct {
r *APIResponse
}
tests := []struct {
name string
args args
want []User
wantErr bool
}{
{
name: "Empty list",
args: args{
r: &APIResponse{
Values: map[string]interface{}{"values": []interface{}{}},
},
},
want: nil,
wantErr: false,
},
{
name: "Bad response",
args: args{
r: &APIResponse{
Values: map[string]interface{}{"values": "not an array"},
},
},
want: nil,
wantErr: true,
},
{
name: "Single group permission",
args: args{
r: &APIResponse{
Values: map[string]interface{}{
"values": []interface{}{
map[string]interface{}{
"name": "jcitizen",
"emailAddress": "jane@example.com",
"id": 101,
"displayName": "Jane Citizen",
"active": true,
"slug": "jcitizen",
"type": "NORMAL",
"directoryName": "Bitbucket Internal Directory",
"deletable": true,
"lastAuthenticationTimestamp": 1368145580548,
"mutableDetails": true,
"mutableGroups": true,
},
},
},
},
},
want: []User{
User{
Name: "jcitizen",
EmailAddress: "jane@example.com",
ID: 101,
DisplayName: "Jane Citizen",
Active: true,
Slug: "jcitizen",
Type: "NORMAL",
DirectoryName: "Bitbucket Internal Directory",
Deletable: true,
LastAuthenticationTimestamp: 1368145580548,
MutableDetails: true,
MutableGroups: true,
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetUsersResponse(tt.args.r)
if err != nil && !tt.wantErr {
t.Errorf("GetGroupsPermissionResponse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetGroupsPermissionResponse() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetGroupsPermissionResponse(t *testing.T) { func TestGetGroupsPermissionResponse(t *testing.T) {
type args struct { type args struct {
r *APIResponse r *APIResponse