-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathapi_keys_test.go
More file actions
155 lines (136 loc) · 5.88 KB
/
api_keys_test.go
File metadata and controls
155 lines (136 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Code generated by oagen. DO NOT EDIT.
package workos_test
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/workos/workos-go/v7"
)
func TestAPIKeys_CreateValidation(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "POST", r.Method)
require.Equal(t, "/api_keys/validations", r.URL.Path)
body, _ := io.ReadAll(r.Body)
var bodyMap map[string]interface{}
require.NoError(t, json.Unmarshal(body, &bodyMap))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fixture, err := os.ReadFile("testdata/api_key_validation_response.json")
if err != nil {
t.Fatalf("failed to read fixture: %v", err)
}
w.Write(fixture)
}))
defer server.Close()
client := workos.NewClient("sk_test", workos.WithBaseURL(server.URL))
result, err := client.APIKeys().CreateValidation(context.Background(), &workos.APIKeysCreateValidationParams{})
require.NoError(t, err)
require.NotNil(t, result)
}
func TestAPIKeys_Delete(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "DELETE", r.Method)
require.Equal(t, "/api_keys/test_id", r.URL.Path)
w.WriteHeader(http.StatusNoContent)
}))
defer server.Close()
client := workos.NewClient("sk_test", workos.WithBaseURL(server.URL))
err := client.APIKeys().Delete(context.Background(), "test_id")
require.NoError(t, err)
}
func TestAPIKeys_ListOrganizationAPIKeys(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "GET", r.Method)
require.Equal(t, "/organizations/test_organizationId/api_keys", r.URL.Path)
require.Equal(t, "10", r.URL.Query().Get("limit"))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fixture, err := os.ReadFile("testdata/list_api_key.json")
if err != nil {
t.Fatalf("failed to read fixture: %v", err)
}
w.Write(fixture)
}))
defer server.Close()
client := workos.NewClient("sk_test", workos.WithBaseURL(server.URL))
iter := client.APIKeys().ListOrganizationAPIKeys(context.Background(), "test_organizationId", &workos.APIKeysListOrganizationAPIKeysParams{PaginationParams: workos.PaginationParams{Limit: ptrInt(10)}})
require.NotNil(t, iter)
require.True(t, iter.Next())
require.NoError(t, iter.Err())
item := iter.Current()
require.NotNil(t, item)
}
func TestAPIKeys_ListOrganizationAPIKeys_Empty(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"data":[],"list_metadata":{"before":null,"after":null}}`))
}))
defer server.Close()
client := workos.NewClient("sk_test", workos.WithBaseURL(server.URL))
iter := client.APIKeys().ListOrganizationAPIKeys(context.Background(), "test_organizationId", &workos.APIKeysListOrganizationAPIKeysParams{PaginationParams: workos.PaginationParams{Limit: ptrInt(10)}})
require.False(t, iter.Next())
require.NoError(t, iter.Err())
}
func TestAPIKeys_CreateOrganizationAPIKey(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "POST", r.Method)
require.Equal(t, "/organizations/test_organizationId/api_keys", r.URL.Path)
body, _ := io.ReadAll(r.Body)
var bodyMap map[string]interface{}
require.NoError(t, json.Unmarshal(body, &bodyMap))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fixture, err := os.ReadFile("testdata/api_key_with_value.json")
if err != nil {
t.Fatalf("failed to read fixture: %v", err)
}
w.Write(fixture)
}))
defer server.Close()
client := workos.NewClient("sk_test", workos.WithBaseURL(server.URL))
result, err := client.APIKeys().CreateOrganizationAPIKey(context.Background(), "test_organizationId", &workos.APIKeysCreateOrganizationAPIKeyParams{})
require.NoError(t, err)
require.NotNil(t, result)
require.Equal(t, "api_key_01EHZNVPK3SFK441A1RGBFSHRT", result.ID)
require.Equal(t, "Production API Key", result.Name)
require.Equal(t, "sk_...3456", result.ObfuscatedValue)
}
func TestAPIKeys_Error401(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"code":"unauthorized","message":"Unauthorized"}`))
}))
defer server.Close()
client := workos.NewClient("sk_test", workos.WithBaseURL(server.URL))
_, err := client.APIKeys().CreateValidation(context.Background(), &workos.APIKeysCreateValidationParams{})
require.IsType(t, &workos.AuthenticationError{}, err)
}
func TestAPIKeys_Error404(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"code":"not_found","message":"Not Found"}`))
}))
defer server.Close()
client := workos.NewClient("sk_test", workos.WithBaseURL(server.URL))
_, err := client.APIKeys().CreateValidation(context.Background(), &workos.APIKeysCreateValidationParams{})
require.IsType(t, &workos.NotFoundError{}, err)
}
func TestAPIKeys_Error422(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(422)
w.Write([]byte(`{"code":"unprocessable_entity","message":"Unprocessable"}`))
}))
defer server.Close()
client := workos.NewClient("sk_test", workos.WithBaseURL(server.URL))
_, err := client.APIKeys().CreateValidation(context.Background(), &workos.APIKeysCreateValidationParams{})
require.IsType(t, &workos.UnprocessableEntityError{}, err)
}