-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_test.go
71 lines (61 loc) · 2.88 KB
/
policy_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
package headscale
import (
"context"
"errors"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestPolicyResource_Get(t *testing.T) {
t.Run("success", func(t *testing.T) {
client := NewMockClient()
policyResource := &PolicyResource{Client: client}
client.(*MockClient).On("buildURL", "policy").Return(&url.URL{Path: "http://example.com/api/v1/policy"})
client.(*MockClient).On("buildRequest", mock.Anything, http.MethodGet, &url.URL{Path: "http://example.com/api/v1/policy"}, requestOptions{}).Return(&http.Request{}, nil)
client.(*MockClient).On("do", mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
resp := args.Get(1).(*Policy)
resp.Policy = "test-policy"
resp.UpdatedAt = "2024-01-01T00:00:00Z"
})
policy, err := policyResource.Get(context.Background())
assert.NoError(t, err)
assert.Equal(t, "test-policy", policy.Policy)
assert.Equal(t, "2024-01-01T00:00:00Z", policy.UpdatedAt)
})
t.Run("error", func(t *testing.T) {
client := NewMockClient()
policyResource := &PolicyResource{Client: client}
client.(*MockClient).On("buildURL", "policy").Return(&url.URL{Path: "http://example.com/api/v1/policy"})
client.(*MockClient).On("buildRequest", mock.Anything, http.MethodGet, &url.URL{Path: "http://example.com/api/v1/policy"}, requestOptions{}).Return(&http.Request{}, nil)
client.(*MockClient).On("do", mock.Anything, mock.Anything).Return(errors.New("request failed"))
policy, err := policyResource.Get(context.Background())
assert.Error(t, err)
assert.Empty(t, policy.Policy)
})
}
func TestPolicyResource_Put(t *testing.T) {
t.Run("success", func(t *testing.T) {
client := NewMockClient()
policyResource := &PolicyResource{Client: client}
client.(*MockClient).On("buildURL", "policy").Return(&url.URL{Path: "http://example.com/api/v1/policy"})
client.(*MockClient).On("buildRequest", mock.Anything, http.MethodPut, &url.URL{Path: "http://example.com/api/v1/policy"}, requestOptions{
body: UpdatePolicyRequest{Policy: "new-policy"},
}).Return(&http.Request{}, nil)
client.(*MockClient).On("do", mock.Anything, mock.Anything).Return(nil)
err := policyResource.Put(context.Background(), "new-policy")
assert.NoError(t, err)
})
t.Run("error", func(t *testing.T) {
client := NewMockClient()
policyResource := &PolicyResource{Client: client}
client.(*MockClient).On("buildURL", "policy").Return(&url.URL{Path: "http://example.com/api/v1/policy"})
client.(*MockClient).On("buildRequest", mock.Anything, http.MethodPut, &url.URL{Path: "http://example.com/api/v1/policy"}, requestOptions{
body: UpdatePolicyRequest{Policy: "new-policy"},
}).Return(&http.Request{}, nil)
client.(*MockClient).On("do", mock.Anything, mock.Anything).Return(errors.New("request failed"))
err := policyResource.Put(context.Background(), "new-policy")
assert.Error(t, err)
})
}