Repository: xanzy/go-gitlab
Pull request #1146 (2021-05-17 13:31:45)

Updated object returned by groups/:id/members calls
Gitlab MR for Object addition for created_at: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/45156

Signed-off-by: Philip Marc Schwartz <Philip.Schwartz1@T-Mobile.com>


Patch:
diff --git a/group_members.go b/group_members.go
index d2d5b50..c00d7ff 100644
--- a/group_members.go
+++ b/group_members.go
@@ -19,6 +19,7 @@ package gitlab
 import (
 	"fmt"
 	"net/http"
+	"time"
 )
 
 // GroupMembersService handles communication with the group members
@@ -50,7 +51,8 @@ type GroupMember struct {
 	State             string                   `json:"state"`
 	AvatarURL         string                   `json:"avatar_url"`
 	WebURL            string                   `json:"web_url"`
-	ExpiresAt         *ISOTime                 `json:"expires_at"`
+	CreatedAt         *time.Time               `json:"created_at"`
+	ExpiresAt         *time.Time               `json:"expires_at"`
 	AccessLevel       AccessLevelValue         `json:"access_level"`
 	GroupSAMLIdentity *GroupMemberSAMLIdentity `json:"group_saml_identity"`
 }


Test patch:
diff --git a/group_members_test.go b/group_members_test.go
index 890597f..98448b5 100644
--- a/group_members_test.go
+++ b/group_members_test.go
@@ -21,6 +21,7 @@ import (
 	"net/http"
 	"reflect"
 	"testing"
+	"time"
 )
 
 func TestListBillableGroupMembers(t *testing.T) {
@@ -30,7 +31,18 @@ func TestListBillableGroupMembers(t *testing.T) {
 	mux.HandleFunc("/api/v4/groups/1/billable_members",
 		func(w http.ResponseWriter, r *http.Request) {
 			testMethod(t, r, http.MethodGet)
-			fmt.Fprint(w, `[{"id":1,"username":"ray","name":"Raymond","state":"active","avatar_url":"https://foo.bar/mypic","web_url":"http://192.168.1.8:3000/root","last_activity_on":"2021-01-27"}]`)
+			fmt.Fprint(w,
+				`[
+					{
+						"id":1,
+						"username":"ray",
+						"name":"Raymond",
+						"state":"active",
+						"avatar_url":"https://foo.bar/mypic",
+						"web_url":"http://192.168.1.8:3000/root",
+						"last_activity_on":"2021-01-27"
+					}
+				]`)
 		})
 
 	billableMembers, _, err := client.Groups.ListBillableGroupMembers(1, &ListBillableGroupMembersOptions{})
@@ -43,8 +55,126 @@ func TestListBillableGroupMembers(t *testing.T) {
 	if err != nil {
 		t.Errorf("Could not unmarshal date string to ISOTime: %v", err)
 	}
-	want := []*BillableGroupMember{{ID: 1, Username: "ray", Name: "Raymond", State: "active", AvatarURL: "https://foo.bar/mypic", WebURL: "http://192.168.1.8:3000/root", LastActivityOn: testTime}}
+	want := []*BillableGroupMember{
+		{
+			ID:             1,
+			Username:       "ray",
+			Name:           "Raymond",
+			State:          "active",
+			AvatarURL:      "https://foo.bar/mypic",
+			WebURL:         "http://192.168.1.8:3000/root",
+			LastActivityOn: testTime,
+		},
+	}
 	if !reflect.DeepEqual(want, billableMembers) {
 		t.Errorf("Groups.ListBillableGroupMembers returned %+v, want %+v", billableMembers, want)
 	}
 }
+
+func TestListGroupMembersWithoutSAML(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/groups/1/members",
+		func(w http.ResponseWriter, r *http.Request) {
+			testMethod(t, r, http.MethodGet)
+			fmt.Fprint(w,
+				`[
+					{
+						"id": 1,
+						"username": "raymond_smith",
+						"name": "Raymond Smith",
+						"state": "active",
+						"avatar_url": "https://www.gravatar.com/avatar/c2525a7f58ae3776070e44c106c48e15?s=80&d=identicon",
+						"web_url": "http://192.168.1.8:3000/root",
+						"created_at": "2012-10-21T14:13:35Z",
+						"expires_at": "2012-10-22T14:13:35Z",
+						"access_level": 30,
+						"group_saml_identity": null
+					}
+				]`)
+		})
+
+	members, _, err := client.Groups.ListGroupMembers(1, &ListGroupMembersOptions{})
+	if err != nil {
+		t.Errorf("Groups.ListGroupMembers returned error: %v", err)
+	}
+
+	createdAt, _ := time.Parse(time.RFC3339, "2012-10-21T14:13:35Z")
+	expiresAt, _ := time.Parse(time.RFC3339, "2012-10-22T14:13:35Z")
+	want := []*GroupMember{
+		{
+			ID:                1,
+			Username:          "raymond_smith",
+			Name:              "Raymond Smith",
+			State:             "active",
+			AvatarURL:         "https://www.gravatar.com/avatar/c2525a7f58ae3776070e44c106c48e15?s=80&d=identicon",
+			WebURL:            "http://192.168.1.8:3000/root",
+			CreatedAt:         &createdAt,
+			ExpiresAt:         &expiresAt,
+			AccessLevel:       30,
+			GroupSAMLIdentity: nil,
+		},
+	}
+	if !reflect.DeepEqual(want, members) {
+		t.Errorf("Groups.ListBillableGroupMembers returned %+v, want %+v", members[0], want[0])
+	}
+}
+
+func TestListGroupMembersWithSAML(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/groups/1/members",
+		func(w http.ResponseWriter, r *http.Request) {
+			testMethod(t, r, http.MethodGet)
+			fmt.Fprint(w,
+				`[
+					{
+						"id": 2,
+						"username": "john_doe",
+						"name": "John Doe",
+						"state": "active",
+						"avatar_url": "https://www.gravatar.com/avatar/c2525a7f58ae3776070e44c106c48e15?s=80&d=identicon",
+						"web_url": "http://192.168.1.8:3000/root",
+						"created_at": "2012-10-21T14:13:35Z",
+						"expires_at": "2012-10-22T14:13:35Z",
+						"access_level": 30,
+						"group_saml_identity": {
+							"extern_uid":"ABC-1234567890",
+							"provider": "group_saml",
+							"saml_provider_id": 10
+						}
+					}
+				]`)
+		})
+
+	members, _, err := client.Groups.ListGroupMembers(1, &ListGroupMembersOptions{})
+	if err != nil {
+		t.Errorf("Groups.ListGroupMembers returned error: %v", err)
+	}
+
+	createdAt, _ := time.Parse(time.RFC3339, "2012-10-21T14:13:35Z")
+	expiresAt, _ := time.Parse(time.RFC3339, "2012-10-22T14:13:35Z")
+	want := []*GroupMember{
+		{
+			ID:          2,
+			Username:    "john_doe",
+			Name:        "John Doe",
+			State:       "active",
+			AvatarURL:   "https://www.gravatar.com/avatar/c2525a7f58ae3776070e44c106c48e15?s=80&d=identicon",
+			WebURL:      "http://192.168.1.8:3000/root",
+			CreatedAt:   &createdAt,
+			ExpiresAt:   &expiresAt,
+			AccessLevel: 30,
+			GroupSAMLIdentity: &GroupMemberSAMLIdentity{
+				ExternUID:      "ABC-1234567890",
+				Provider:       "group_saml",
+				SAMLProviderID: 10,
+			},
+		},
+	}
+	if !reflect.DeepEqual(want, members) {
+		t.Errorf("Groups.ListBillableGroupMembers returned %+v, want %+v", members[0], want[0])
+	}
+}