-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
147 lines (127 loc) · 4.89 KB
/
service_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
146
147
package rocketoff
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/PRAgarawal/rocketoff/chat"
kitlog "github.com/go-kit/kit/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
const (
testWebhook = "https://clutchCITY.net"
testUser = "Rudy T"
redirectURI = "https://redirect.uri"
)
func TestShowEmTheBeard(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
msgr := &mockMessenger{}
svc := New(kitlog.NewNopLogger(), msgr, nil)
shouldAccept := &chat.CommandReply{
WebhookURL: testWebhook,
RequestingUserID: testUser,
ImageURL: theBeardGif,
}
msgr.On("SendImageReply", shouldAccept).Return(nil)
command := &ImageCommand{
WebhookURL: testWebhook,
RequestingUserID: testUser,
}
assert.NoError(t, svc.ShowEmTheBeard(context.Background(), command))
})
t.Run("messenger error", func(t *testing.T) {
msgr := &mockMessenger{}
svc := New(kitlog.NewNopLogger(), msgr, nil)
msgr.On("SendImageReply", mock.Anything).Return(fmt.Errorf("A BASIC ASS CASUAL POSTED A YOUTUBE CLIP TO PROVE HARDEN PLAYS NO DEFENSE AND FLOPS"))
err := svc.ShowEmTheBeard(context.Background(), &ImageCommand{})
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "BASIC ASS")
}
})
}
func TestShowEmThePointGod(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
msgr := &mockMessenger{}
svc := New(kitlog.NewNopLogger(), msgr, nil)
shouldAccept := &chat.CommandReply{
WebhookURL: testWebhook,
RequestingUserID: testUser,
ImageURL: thePointGodGif,
}
msgr.On("SendImageReply", shouldAccept).Return(nil)
command := &ImageCommand{
WebhookURL: testWebhook,
RequestingUserID: testUser,
}
assert.NoError(t, svc.ShowEmThePointGod(context.Background(), command))
})
t.Run("messenger error", func(t *testing.T) {
msgr := &mockMessenger{}
svc := New(kitlog.NewNopLogger(), msgr, nil)
msgr.On("SendImageReply", mock.Anything).Return(fmt.Errorf("OH GOD WESTBROOK IS OPEN FROM 18 FEET"))
err := svc.ShowEmThePointGod(context.Background(), &ImageCommand{})
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "WESTBROOK IS OPEN")
}
})
}
func TestCompleteChatOAuth(t *testing.T) {
// Set up a service to respond to authorization and token exchange requests in these tests
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.String() != "/token" {
t.Errorf("Unexpected exchange request URL %q", r.URL)
}
headerAuth := r.Header.Get("Authorization")
assert.Equal(t, "Basic MzozMzM=", headerAuth, fmt.Sprintf("Unexpected authorization header %q, want %q", headerAuth, "Basic MzozMzM="))
headerContentType := r.Header.Get("Content-Type")
assert.Equal(t, "application/x-www-form-urlencoded", headerContentType, fmt.Sprintf("Unexpected Content-Type header %q", headerContentType))
body, err := ioutil.ReadAll(r.Body)
assert.Nil(t, err, "Failed reading request body")
assert.Equal(t, "code=code&grant_type=authorization_code", string(body), fmt.Sprintf("Unexpected exchange payload; got %q", body))
w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
w.Write([]byte("access_token=90d64460d14870c08c81352a05dedd3465940a7c&scope=user&token_type=bearer&refresh_token=80d64460d14870c07c81353a05dedd3465940a7d&expires_in=3600"))
}))
defer ts.Close()
t.Run("happy path - completing chat OAuth", func(t *testing.T) {
config := &ChatConfig{
ClientID: "3",
ClientSecret: "333",
TokenEndpoint: ts.URL + "/token",
OAuthCompleteRedirectURL: redirectURI,
}
svc := New(kitlog.NewNopLogger(), nil, config)
oauthOptions := &OAuthCompleteOptions{Code: "code"}
redirect, err := svc.CompleteChatOAuth(context.Background(), oauthOptions)
assert.Nil(t, err)
assert.Equal(t, redirectURI, redirect)
})
}
func TestRedirectForOAuth(t *testing.T) {
config := &ChatConfig{
ClientID: "123",
AuthorizationEndpoint: "https://authorize.me/",
OAuthRedirectURL: "localhost/oauth_complete",
Scopes: "scope1,scope2",
}
svc := New(kitlog.NewNopLogger(), nil, config)
expectedURI := "https://authorize.me/?client_id=123&redirect_uri=localhost%2Foauth_complete&response_type=code&scope=scope1+scope2"
redirect, err := svc.RedirectForOAuth(context.Background())
assert.Equal(t, expectedURI, redirect)
assert.Nil(t, err)
}
func TestBuildRedirectURI(t *testing.T) {
baseURI := "http://redirect.uri?key1=value1&key2=value2"
resultURI, err := buildRedirectURI(baseURI, ErrInvalidValue{"WE'RE ALL GONNA DIE"})
assert.Nil(t, err)
assert.Equal(t, "http://redirect.uri?error=invalid+value%3A+%27WE%27RE+ALL+GONNA+DIE%27&key1=value1&key2=value2", resultURI)
}
type mockMessenger struct {
mock.Mock
}
func (m *mockMessenger) SendImageReply(reply *chat.CommandReply) error {
args := m.Called(reply)
return args.Error(0)
}