Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2059 from jeremad/jrm/email_domains
Browse files Browse the repository at this point in the history
feat: implement 'allowed_email_domains_list' group attribute
  • Loading branch information
timofurrer authored Nov 15, 2024
2 parents 8eaea58 + 4f7ea73 commit 24eba26
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
3 changes: 2 additions & 1 deletion groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Group struct {
MarkedForDeletionOn *ISOTime `json:"marked_for_deletion_on"`
CreatedAt *time.Time `json:"created_at"`
IPRestrictionRanges string `json:"ip_restriction_ranges"`
AllowedEmailDomainsList string `json:"allowed_email_domains_list"`
WikiAccessLevel AccessControlValue `json:"wiki_access_level"`

// Deprecated: Use EmailsEnabled instead
Expand Down Expand Up @@ -383,7 +384,6 @@ type CreateGroupOptions struct {
ParentID *int `url:"parent_id,omitempty" json:"parent_id,omitempty"`
SharedRunnersMinutesLimit *int `url:"shared_runners_minutes_limit,omitempty" json:"shared_runners_minutes_limit,omitempty"`
ExtraSharedRunnersMinutesLimit *int `url:"extra_shared_runners_minutes_limit,omitempty" json:"extra_shared_runners_minutes_limit,omitempty"`
IPRestrictionRanges *string `url:"ip_restriction_ranges,omitempty" json:"ip_restriction_ranges,omitempty"`
WikiAccessLevel *AccessControlValue `url:"wiki_access_level,omitempty" json:"wiki_access_level,omitempty"`

// Deprecated: Use EmailsEnabled instead
Expand Down Expand Up @@ -532,6 +532,7 @@ type UpdateGroupOptions struct {
SharedRunnersSetting *SharedRunnersSettingValue `url:"shared_runners_setting,omitempty" json:"shared_runners_setting,omitempty"`
PreventSharingGroupsOutsideHierarchy *bool `url:"prevent_sharing_groups_outside_hierarchy,omitempty" json:"prevent_sharing_groups_outside_hierarchy,omitempty"`
IPRestrictionRanges *string `url:"ip_restriction_ranges,omitempty" json:"ip_restriction_ranges,omitempty"`
AllowedEmailDomainsList *string `url:"allowed_email_domains_list,omitempty" json:"allowed_email_domains_list,omitempty"`
WikiAccessLevel *AccessControlValue `url:"wiki_access_level,omitempty" json:"wiki_access_level,omitempty"`

// Deprecated: Use EmailsEnabled instead
Expand Down
86 changes: 58 additions & 28 deletions groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,49 +783,40 @@ func TestUnshareGroupFromGroup(t *testing.T) {
}
}

func TestCreateGroupWithIPRestrictionRanges(t *testing.T) {
func TestUpdateGroupWithIPRestrictionRanges(t *testing.T) {
mux, client := setup(t)
const ipRange = "192.168.0.0/24"

mux.HandleFunc("/api/v4/groups",
mux.HandleFunc("/api/v4/groups/1",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(w, `{"id": 1, "name": "g", "path": "g", "ip_restriction_ranges" : "192.168.0.0/24"}`)
})

opt := &CreateGroupOptions{
Name: Ptr("g"),
Path: Ptr("g"),
IPRestrictionRanges: Ptr("192.168.0.0/24"),
}
testMethod(t, r, http.MethodPut)

group, _, err := client.Groups.CreateGroup(opt, nil)
if err != nil {
t.Errorf("Groups.CreateGroup returned error: %v", err)
}
body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("Failed to read the request body. Error: %v", err)
}

want := &Group{ID: 1, Name: "g", Path: "g", IPRestrictionRanges: "192.168.0.0/24"}
if !reflect.DeepEqual(want, group) {
t.Errorf("Groups.CreateGroup returned %+v, want %+v", group, want)
}
}
var bodyJson map[string]interface{}
err = json.Unmarshal(body, &bodyJson)
if err != nil {
t.Fatalf("Failed to parse the request body into JSON. Error: %v", err)
}

func TestUpdateGroupWithIPRestrictionRanges(t *testing.T) {
mux, client := setup(t)
if bodyJson["ip_restriction_ranges"] != ipRange {
t.Fatalf("Test failed. `ip_restriction_ranges` expected to be '%v', got %v", ipRange, bodyJson["ip_restriction_ranges"])
}

mux.HandleFunc("/api/v4/groups/1",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
fmt.Fprint(w, `{"id": 1, "ip_restriction_ranges" : "192.168.0.0/24"}`)
fmt.Fprintf(w, `{"id": 1, "ip_restriction_ranges" : "%v"}`, ipRange)
})

group, _, err := client.Groups.UpdateGroup(1, &UpdateGroupOptions{
IPRestrictionRanges: Ptr("192.168.0.0/24"),
IPRestrictionRanges: Ptr(ipRange),
})
if err != nil {
t.Errorf("Groups.UpdateGroup returned error: %v", err)
}

want := &Group{ID: 1, IPRestrictionRanges: "192.168.0.0/24"}
want := &Group{ID: 1, IPRestrictionRanges: ipRange}
if !reflect.DeepEqual(want, group) {
t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", group, want)
}
Expand Down Expand Up @@ -1124,3 +1115,42 @@ func TestEditGroupPushRules(t *testing.T) {
t.Errorf("Groups.EditGroupPushRule returned %+v, want %+v", rule, want)
}
}

func TestUpdateGroupWithAllowedEmailDomainsList(t *testing.T) {
mux, client := setup(t)
const domain = "example.com"

mux.HandleFunc("/api/v4/groups/1",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)

body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("Failed to read the request body. Error: %v", err)
}

var bodyJson map[string]interface{}
err = json.Unmarshal(body, &bodyJson)
if err != nil {
t.Fatalf("Failed to parse the request body into JSON. Error: %v", err)
}

if bodyJson["allowed_email_domains_list"] != domain {
t.Fatalf("Test failed. `allowed_email_domains_list` expected to be '%v', got %v", domain, bodyJson["allowed_email_domains_list"])
}

fmt.Fprintf(w, `{"id": 1, "allowed_email_domains_list" : "%v"}`, domain)
})

group, _, err := client.Groups.UpdateGroup(1, &UpdateGroupOptions{
AllowedEmailDomainsList: Ptr(domain),
})
if err != nil {
t.Errorf("Groups.UpdateGroup returned error: %v", err)
}

want := &Group{ID: 1, AllowedEmailDomainsList: domain}
if !reflect.DeepEqual(want, group) {
t.Errorf("Groups.UpdatedGroup returned %+v, want %+v", group, want)
}
}

0 comments on commit 24eba26

Please sign in to comment.