-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
145 lines (124 loc) · 4.19 KB
/
session_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
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
package redash_test
import (
"context"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/winebarrel/redash-go/v2"
)
func Test_TestCredentials_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/session", func(req *http.Request) (*http.Response, error) {
assert.Equal(
http.Header(
http.Header{
"Authorization": []string{"Key " + testRedashAPIKey},
"Content-Type": []string{"application/json"},
"User-Agent": []string{"redash-go"},
},
),
req.Header,
)
return httpmock.NewStringResponse(http.StatusOK, `{}`), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
err := client.TestCredentials(context.Background())
assert.NoError(err)
}
func Test_TestCredentials_Err(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/session", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusNotFound, `{"message": "Couldn't find resource. Please login and try again."}`), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
err := client.TestCredentials(context.Background())
assert.ErrorContains(err, "GET api/session failed: HTTP status code not OK: 404")
}
func Test_TestCredentials_Acc(t *testing.T) {
if !testAcc {
t.Skip()
}
assert := assert.New(t)
client, _ := redash.NewClient(testRedashEndpoint, testRedashAPIKey)
err := client.TestCredentials(context.Background())
assert.NoError(err)
}
func Test_GetSession_OK(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/session", func(req *http.Request) (*http.Response, error) {
assert.Equal(
http.Header(
http.Header{
"Authorization": []string{"Key " + testRedashAPIKey},
"Content-Type": []string{"application/json"},
"User-Agent": []string{"redash-go"},
},
),
req.Header,
)
return httpmock.NewStringResponse(http.StatusOK, `
{
"client_config": {},
"messages": [
"email-not-verified",
"using-deprecated-embed-feature"
],
"org_slug": "default",
"user": {}
}
`), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
res, err := client.GetSession(context.Background())
assert.NoError(err)
assert.Equal(&redash.Session{
ClientConfig: redash.ClientConfig{},
Messages: []string{
"email-not-verified",
"using-deprecated-embed-feature",
},
OrgSlug: "default",
User: redash.User{},
}, res)
}
func Test_GetSession_Err_5xx(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/session", func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusServiceUnavailable, "error"), nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.GetSession(context.Background())
assert.ErrorContains(err, "GET api/session failed: HTTP status code not OK: 503\nerror")
}
func Test_GetSession_IOErr(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://redash.example.com/api/session", func(req *http.Request) (*http.Response, error) {
return testIOErrResp, nil
})
client, _ := redash.NewClient("https://redash.example.com", testRedashAPIKey)
_, err := client.GetSession(context.Background())
assert.ErrorContains(err, "Read response body failed: IO error")
}
func Test_Session_Acc(t *testing.T) {
if !testAcc {
t.Skip()
}
assert := assert.New(t)
require := require.New(t)
client, _ := redash.NewClient(testRedashEndpoint, testRedashAPIKey)
session, err := client.GetSession(context.Background())
require.NoError(err)
assert.Equal("admin", session.User.Name)
}