|
| 1 | +// Copyright 2025 The go-github AUTHORS. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by a BSD-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | + |
| 6 | +package github |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/google/go-cmp/cmp" |
| 15 | +) |
| 16 | + |
| 17 | +func TestEnterpriseService_GetConsumedLicenses(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + client, mux, _ := setup(t) |
| 20 | + |
| 21 | + mux.HandleFunc("/enterprises/e/consumed-licenses", func(w http.ResponseWriter, r *http.Request) { |
| 22 | + testMethod(t, r, "GET") |
| 23 | + testFormValues(t, r, values{"page": "2", "per_page": "10"}) |
| 24 | + fmt.Fprint(w, `{ |
| 25 | + "total_seats_consumed": 20, |
| 26 | + "total_seats_purchased": 25, |
| 27 | + "users": [{ |
| 28 | + "github_com_login": "user1", |
| 29 | + "github_com_name": "User One", |
| 30 | + "enterprise_server_user_ids": ["123", "456"], |
| 31 | + "github_com_user": true, |
| 32 | + "enterprise_server_user": false, |
| 33 | + "visual_studio_subscription_user": false, |
| 34 | + "license_type": "Enterprise", |
| 35 | + "github_com_profile": "https://github.com/user1", |
| 36 | + "github_com_member_roles": ["member"], |
| 37 | + "github_com_enterprise_roles": ["member"], |
| 38 | + "github_com_verified_domain_emails": ["[email protected]"], |
| 39 | + "github_com_saml_name_id": "saml123", |
| 40 | + "github_com_orgs_with_pending_invites": [], |
| 41 | + "github_com_two_factor_auth": ["enabled"], |
| 42 | + "enterprise_server_emails": ["[email protected]"], |
| 43 | + "visual_studio_license_status": "active", |
| 44 | + "visual_studio_subscription_email": "[email protected]", |
| 45 | + "total_user_accounts": 1, |
| 46 | + "github_com_enterprise_role": "owner" |
| 47 | + }] |
| 48 | + }`) |
| 49 | + }) |
| 50 | + |
| 51 | + opt := &ListOptions{Page: 2, PerPage: 10} |
| 52 | + ctx := t.Context() |
| 53 | + licenses, _, err := client.Enterprise.GetConsumedLicenses(ctx, "e", opt) |
| 54 | + if err != nil { |
| 55 | + t.Errorf("Enterprise.GetConsumedLicenses returned error: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + want := &EnterpriseConsumedLicenses{ |
| 59 | + TotalSeatsConsumed: 20, |
| 60 | + TotalSeatsPurchased: 25, |
| 61 | + Users: []*EnterpriseLicensedUsers{ |
| 62 | + { |
| 63 | + GithubComLogin: "user1", |
| 64 | + GithubComName: "User One", |
| 65 | + EnterpriseServerUserIDs: []string{"123", "456"}, |
| 66 | + GithubComUser: true, |
| 67 | + EnterpriseServerUser: false, |
| 68 | + VisualStudioSubscriptionUser: false, |
| 69 | + LicenseType: "Enterprise", |
| 70 | + GithubComProfile: "https://github.com/user1", |
| 71 | + GithubComMemberRoles: []string{"member"}, |
| 72 | + GithubComEnterpriseRoles: []string{"member"}, |
| 73 | + GithubComVerifiedDomainEmails: [] string{ "[email protected]"}, |
| 74 | + GithubComSamlNameID: "saml123", |
| 75 | + GithubComOrgsWithPendingInvites: []string{}, |
| 76 | + GithubComTwoFactorAuth: []string{"enabled"}, |
| 77 | + EnterpriseServerEmails: [] string{ "[email protected]"}, |
| 78 | + VisualStudioLicenseStatus: "active", |
| 79 | + VisualStudioSubscriptionEmail: "[email protected]", |
| 80 | + TotalUserAccounts: 1, |
| 81 | + GithubComEnterpriseRole: "owner", |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + if !cmp.Equal(licenses, want) { |
| 87 | + t.Errorf("Enterprise.GetConsumedLicenses returned %+v, want %+v", licenses, want) |
| 88 | + } |
| 89 | + |
| 90 | + const methodName = "GetConsumedLicenses" |
| 91 | + testBadOptions(t, methodName, func() (err error) { |
| 92 | + _, _, err = client.Enterprise.GetConsumedLicenses(ctx, "\n", opt) |
| 93 | + return err |
| 94 | + }) |
| 95 | + |
| 96 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 97 | + got, resp, err := client.Enterprise.GetConsumedLicenses(ctx, "e", opt) |
| 98 | + if got != nil { |
| 99 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 100 | + } |
| 101 | + return resp, err |
| 102 | + }) |
| 103 | +} |
| 104 | + |
| 105 | +func TestEnterpriseService_GetLicenseSyncStatus(t *testing.T) { |
| 106 | + t.Parallel() |
| 107 | + client, mux, _ := setup(t) |
| 108 | + |
| 109 | + mux.HandleFunc("/enterprises/e/license-sync-status", func(w http.ResponseWriter, r *http.Request) { |
| 110 | + testMethod(t, r, "GET") |
| 111 | + fmt.Fprint(w, `{ |
| 112 | + "title": "Enterprise License Sync Status", |
| 113 | + "description": "Status of license synchronization", |
| 114 | + "properties": { |
| 115 | + "server_instances": { |
| 116 | + "type": "array", |
| 117 | + "items": { |
| 118 | + "type": "object", |
| 119 | + "properties": { |
| 120 | + "server_id": "ghes-1", |
| 121 | + "hostname": "github.enterprise.local", |
| 122 | + "last_sync": { |
| 123 | + "type": "object", |
| 124 | + "properties": { |
| 125 | + "date": "2025-10-30T10:30:00Z", |
| 126 | + "status": "success", |
| 127 | + "error": "" |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + }`) |
| 135 | + }) |
| 136 | + |
| 137 | + ctx := t.Context() |
| 138 | + syncStatus, _, err := client.Enterprise.GetLicenseSyncStatus(ctx, "e") |
| 139 | + if err != nil { |
| 140 | + t.Errorf("Enterprise.GetLicenseSyncStatus returned error: %v", err) |
| 141 | + } |
| 142 | + |
| 143 | + want := &EnterpriseLicenseSyncStatus{ |
| 144 | + Title: "Enterprise License Sync Status", |
| 145 | + Description: "Status of license synchronization", |
| 146 | + Properties: &ServerInstanceProperties{ |
| 147 | + ServerInstances: &ServerInstances{ |
| 148 | + Type: "array", |
| 149 | + Items: &ServiceInstanceItems{ |
| 150 | + Type: "object", |
| 151 | + Properties: &ServerItemProperties{ |
| 152 | + ServerID: "ghes-1", |
| 153 | + Hostname: "github.enterprise.local", |
| 154 | + LastSync: &LastLicenseSync{ |
| 155 | + Type: "object", |
| 156 | + Properties: &LastLicenseSyncProperties{ |
| 157 | + Date: &Timestamp{time.Date(2025, 10, 30, 10, 30, 0, 0, time.UTC)}, |
| 158 | + Status: "success", |
| 159 | + Error: "", |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + fmt.Printf("%v\n", cmp.Diff(want, syncStatus)) |
| 169 | + |
| 170 | + if !cmp.Equal(syncStatus, want) { |
| 171 | + t.Errorf("Enterprise.GetLicenseSyncStatus returned %+v, want %+v", syncStatus, want) |
| 172 | + } |
| 173 | + |
| 174 | + const methodName = "GetLicenseSyncStatus" |
| 175 | + testBadOptions(t, methodName, func() (err error) { |
| 176 | + _, _, err = client.Enterprise.GetLicenseSyncStatus(ctx, "\n") |
| 177 | + return err |
| 178 | + }) |
| 179 | + |
| 180 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 181 | + got, resp, err := client.Enterprise.GetLicenseSyncStatus(ctx, "e") |
| 182 | + if got != nil { |
| 183 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 184 | + } |
| 185 | + return resp, err |
| 186 | + }) |
| 187 | +} |
0 commit comments