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