-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_list_test.go
More file actions
130 lines (107 loc) · 3.18 KB
/
source_list_test.go
File metadata and controls
130 lines (107 loc) · 3.18 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
package unstructured
import (
"errors"
"net/http"
"strconv"
"strings"
"testing"
"time"
)
func TestListSources(t *testing.T) {
t.Parallel()
client, mux := testclient(t)
mux.ListSources = func(w http.ResponseWriter, _ *http.Request) {
response := []byte(`[` +
` {` +
` "config": {` +
` "client_id": "foo",` +
` "tenant": "foo",` +
` "authority_url": "foo",` +
` "user_pname": "foo",` +
` "client_cred": "foo",` +
` "recursive": false,` +
` "path": "foo"` +
` },` +
` "created_at": "2023-09-15T01:06:53.146Z",` +
` "id": "a15d4161-77a0-4e08-b65e-86f398ce15ad",` +
` "name": "test_source_name",` +
` "type": "onedrive"` +
` }` +
`]`)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.Write(response)
}
sources, err := client.ListSources(testContext(t), "")
if err != nil {
t.Fatalf("failed to list sources: %v", err)
}
if len(sources) != 1 {
t.Fatalf("expected 1 source, got %d", len(sources))
}
source := sources[0]
if err := errors.Join(
eq("source.id", source.ID, "a15d4161-77a0-4e08-b65e-86f398ce15ad"),
eq("source.name", source.Name, "test_source_name"),
equal("source.created_at", source.CreatedAt, time.Date(2023, 9, 15, 1, 6, 53, 146000000, time.UTC)),
); err != nil {
t.Error(err)
}
cfg, ok := source.Config.(*OneDriveConnectorConfig)
if !ok {
t.Errorf("expected source config to be %T, got %T", cfg, source.Config)
}
}
func TestListSourcesEmpty(t *testing.T) {
t.Parallel()
client, mux := testclient(t)
mux.ListSources = func(w http.ResponseWriter, _ *http.Request) {
response := []byte(`[]`)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.Write(response)
}
sources, err := client.ListSources(testContext(t), "")
if err != nil {
t.Fatalf("failed to list sources: %v", err)
}
if len(sources) != 0 {
t.Fatalf("expected 0 sources, got %d", len(sources))
}
}
func TestListSourcesErrorCode(t *testing.T) {
t.Parallel()
for _, code := range []int{
http.StatusBadRequest, // 400
http.StatusUnauthorized, // 401
http.StatusForbidden, // 403
http.StatusNotFound, // 404
http.StatusInternalServerError, // 500
http.StatusBadGateway, // 502
http.StatusServiceUnavailable, // 503
http.StatusGatewayTimeout, // 504
} {
t.Run(strconv.Itoa(code), func(t *testing.T) {
t.Parallel()
client, mux := testclient(t)
mux.ListSources = func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
}
_, err := client.ListSources(testContext(t), "")
if err == nil {
t.Fatalf("expected error, got nil")
}
if !strings.Contains(err.Error(), "API error occurred") {
t.Fatalf("expected error to contain 'API error occurred', got %v", err)
}
var apierr *APIError
if !errors.As(err, &apierr) {
t.Fatalf("expected error to be an %T, got %T", apierr, err)
}
if apierr.Code != code {
t.Fatalf("expected error code to be %d, got %d", code, apierr.Code)
}
})
}
}