|
| 1 | +package openai_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/sashabaranov/go-openai" |
| 11 | + "github.com/sashabaranov/go-openai/internal/test/checks" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAdminInvite(t *testing.T) { |
| 15 | + adminInviteObject := "organization.invite" |
| 16 | + adminInviteID := "invite-abc-123" |
| 17 | + adminInviteEmail := "invite@openai.com" |
| 18 | + adminInviteRole := "owner" |
| 19 | + adminInviteStatus := "pending" |
| 20 | + |
| 21 | + adminInviteInvitedAt := int64(1711471533) |
| 22 | + adminInviteExpiresAt := int64(1711471533) |
| 23 | + adminInviteAcceptedAt := int64(1711471533) |
| 24 | + adminInviteProjects := []openai.AdminInviteProject{ |
| 25 | + { |
| 26 | + ID: "project-id", |
| 27 | + Role: "owner", |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + client, server, teardown := setupOpenAITestServer() |
| 32 | + defer teardown() |
| 33 | + |
| 34 | + server.RegisterHandler( |
| 35 | + "/v1/organization/invites", |
| 36 | + func(w http.ResponseWriter, r *http.Request) { |
| 37 | + switch r.Method { |
| 38 | + case http.MethodGet: |
| 39 | + resBytes, _ := json.Marshal(openai.AdminInviteList{ |
| 40 | + Object: "list", |
| 41 | + AdminInvites: []openai.AdminInvite{ |
| 42 | + { |
| 43 | + Object: adminInviteObject, |
| 44 | + ID: adminInviteID, |
| 45 | + Email: adminInviteEmail, |
| 46 | + Role: adminInviteRole, |
| 47 | + Status: adminInviteStatus, |
| 48 | + InvitedAt: adminInviteInvitedAt, |
| 49 | + ExpiresAt: adminInviteExpiresAt, |
| 50 | + AcceptedAt: adminInviteAcceptedAt, |
| 51 | + Projects: adminInviteProjects, |
| 52 | + }, |
| 53 | + }, |
| 54 | + FirstID: "first_id", |
| 55 | + LastID: "last_id", |
| 56 | + HasMore: false, |
| 57 | + }) |
| 58 | + fmt.Fprintln(w, string(resBytes)) |
| 59 | + |
| 60 | + case http.MethodPost: |
| 61 | + resBytes, _ := json.Marshal(openai.AdminInvite{ |
| 62 | + Object: adminInviteObject, |
| 63 | + ID: adminInviteID, |
| 64 | + Email: adminInviteEmail, |
| 65 | + Role: adminInviteRole, |
| 66 | + Status: adminInviteStatus, |
| 67 | + InvitedAt: adminInviteInvitedAt, |
| 68 | + ExpiresAt: adminInviteExpiresAt, |
| 69 | + AcceptedAt: adminInviteAcceptedAt, |
| 70 | + Projects: adminInviteProjects, |
| 71 | + }) |
| 72 | + fmt.Fprintln(w, string(resBytes)) |
| 73 | + } |
| 74 | + }, |
| 75 | + ) |
| 76 | + |
| 77 | + server.RegisterHandler( |
| 78 | + "/v1/organization/invites/"+adminInviteID, |
| 79 | + func(w http.ResponseWriter, r *http.Request) { |
| 80 | + switch r.Method { |
| 81 | + case http.MethodDelete: |
| 82 | + resBytes, _ := json.Marshal(openai.AdminInviteDeleteResponse{ |
| 83 | + ID: adminInviteID, |
| 84 | + Object: adminInviteObject, |
| 85 | + Deleted: true, |
| 86 | + }) |
| 87 | + fmt.Fprintln(w, string(resBytes)) |
| 88 | + |
| 89 | + case http.MethodGet: |
| 90 | + resBytes, _ := json.Marshal(openai.AdminInvite{ |
| 91 | + Object: adminInviteObject, |
| 92 | + ID: adminInviteID, |
| 93 | + Email: adminInviteEmail, |
| 94 | + Role: adminInviteRole, |
| 95 | + Status: adminInviteStatus, |
| 96 | + InvitedAt: adminInviteInvitedAt, |
| 97 | + ExpiresAt: adminInviteExpiresAt, |
| 98 | + AcceptedAt: adminInviteAcceptedAt, |
| 99 | + Projects: adminInviteProjects, |
| 100 | + }) |
| 101 | + fmt.Fprintln(w, string(resBytes)) |
| 102 | + } |
| 103 | + }, |
| 104 | + ) |
| 105 | + |
| 106 | + ctx := context.Background() |
| 107 | + |
| 108 | + t.Run("ListAdminInvites", func(t *testing.T) { |
| 109 | + adminInvites, err := client.ListAdminInvites(ctx, nil, nil) |
| 110 | + checks.NoError(t, err, "ListAdminInvites error") |
| 111 | + |
| 112 | + if len(adminInvites.AdminInvites) != 1 { |
| 113 | + t.Fatalf("expected 1 admin invite, got %d", len(adminInvites.AdminInvites)) |
| 114 | + } |
| 115 | + |
| 116 | + if adminInvites.AdminInvites[0].ID != adminInviteID { |
| 117 | + t.Errorf("expected admin invite ID %s, got %s", adminInviteID, adminInvites.AdminInvites[0].ID) |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + t.Run("CreateAdminInvite", func(t *testing.T) { |
| 122 | + adminInvite, err := client.CreateAdminInvite(ctx, adminInviteEmail, adminInviteRole, &adminInviteProjects) |
| 123 | + checks.NoError(t, err, "CreateAdminInvite error") |
| 124 | + |
| 125 | + if adminInvite.ID != adminInviteID { |
| 126 | + t.Errorf("expected admin invite ID %s, got %s", adminInviteID, adminInvite.ID) |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + t.Run("RetrieveAdminInvite", func(t *testing.T) { |
| 131 | + adminInvite, err := client.RetrieveAdminInvite(ctx, adminInviteID) |
| 132 | + checks.NoError(t, err, "RetrieveAdminInvite error") |
| 133 | + |
| 134 | + if adminInvite.ID != adminInviteID { |
| 135 | + t.Errorf("expected admin invite ID %s, got %s", adminInviteID, adminInvite.ID) |
| 136 | + } |
| 137 | + }) |
| 138 | + |
| 139 | + t.Run("DeleteAdminInvite", func(t *testing.T) { |
| 140 | + adminInviteDeleteResponse, err := client.DeleteAdminInvite(ctx, adminInviteID) |
| 141 | + checks.NoError(t, err, "DeleteAdminInvite error") |
| 142 | + |
| 143 | + if !adminInviteDeleteResponse.Deleted { |
| 144 | + t.Errorf("expected admin invite to be deleted, got not deleted") |
| 145 | + } |
| 146 | + }) |
| 147 | +} |
0 commit comments