-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_test.go
115 lines (99 loc) · 2.67 KB
/
user_test.go
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
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-Present Datadog, Inc.
package cloudcraft_test
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"reflect"
"testing"
"github.com/DataDog/cloudcraft-go"
"github.com/DataDog/cloudcraft-go/internal/xtesting"
)
const _testUserDataPath string = "tests/data/user"
func TestUserService_Me(t *testing.T) {
t.Parallel()
var (
validTestData = xtesting.ReadFile(t, filepath.Join(_testUserDataPath, "me-valid.json"))
invalidTestData = xtesting.ReadFile(t, filepath.Join(_testUserDataPath, "me-invalid.json"))
ctx = context.Background()
)
tests := []struct {
name string
handler http.HandlerFunc
context context.Context
want *cloudcraft.User
wantErr bool
}{
{
name: "Valid user data",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(validTestData)
},
context: ctx,
want: &cloudcraft.User{
ID: "b92570ba-8969-4e41-b6a3-3d672b44f9f5",
Name: "Go SDK",
Email: "[email protected]",
Settings: map[string]any{
"currency": "USD",
"firstTime": false,
},
CreatedAt: xtesting.ParseTime(t, "2022-10-10T16:52:40.771Z"),
UpdatedAt: xtesting.ParseTime(t, "2023-11-08T14:44:28.872Z"),
AccessedAt: xtesting.ParseTime(t, "2023-11-08T14:44:28.872Z"),
},
wantErr: false,
},
{
name: "Invalid user data",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(invalidTestData)
},
context: ctx,
want: nil,
wantErr: true,
},
{
name: "API error response",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
},
context: ctx,
want: nil,
wantErr: true,
},
{
name: "Nil context",
handler: func(_ http.ResponseWriter, _ *http.Request) {},
context: nil,
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
server := httptest.NewServer(tt.handler)
defer server.Close()
endpoint, err := url.Parse(server.URL)
if err != nil {
t.Fatal(err)
}
client := xtesting.SetupMockClient(t, endpoint)
got, _, err := client.User.Me(tt.context)
if (err != nil) != tt.wantErr {
t.Fatalf("UserService.Me() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && !reflect.DeepEqual(got, tt.want) {
t.Fatalf("UserService.Me() = %v, want %v", got, tt.want)
}
})
}
}