Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 03c0525

Browse files
author
Noah Hanjun Lee
authored
fix: add unit tests for Slack (#85)
* Fix to use constructor * Add unit tests for interaction
1 parent 2fbb902 commit 03c0525

File tree

8 files changed

+539
-173
lines changed

8 files changed

+539
-173
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ require (
3333
go.uber.org/zap v1.13.0
3434
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
3535
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
36+
gopkg.in/h2non/gock.v1 v1.1.2 // indirect
3637
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
3738
)

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
199199
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
200200
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
201201
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
202+
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
203+
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
202204
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
203205
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
204206
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
@@ -340,6 +342,7 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
340342
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
341343
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
342344
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
345+
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
343346
github.com/nleeper/goment v1.4.2 h1:r4c8KkCrsBJUnVi/IJ5HEqev5QY8aCWOXQtu+eYXtnI=
344347
github.com/nleeper/goment v1.4.2/go.mod h1:zDl5bAyDhqxwQKAvkSXMRLOdCowrdZz53ofRJc4VhTo=
345348
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
@@ -758,6 +761,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
758761
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
759762
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
760763
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
764+
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
765+
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
761766
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
762767
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
763768
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=

internal/server/slack/deploy.go

Lines changed: 54 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -173,109 +173,64 @@ func buildDeployView(callbackID string, c *vo.Config, perms []*ent.Perm) slack.M
173173
return slack.ModalViewRequest{
174174
Type: slack.VTModal,
175175
CallbackID: callbackID,
176-
Title: &slack.TextBlockObject{
177-
Type: slack.PlainTextType,
178-
Text: "Deploy",
179-
},
180-
Submit: &slack.TextBlockObject{
181-
Type: slack.PlainTextType,
182-
Text: "Submit",
183-
},
184-
Close: &slack.TextBlockObject{
185-
Type: slack.PlainTextType,
186-
Text: "Close",
187-
},
176+
Title: slack.NewTextBlockObject(slack.PlainTextType, "Deploy", false, false),
177+
Submit: slack.NewTextBlockObject(slack.PlainTextType, "Submit", false, false),
178+
Close: slack.NewTextBlockObject(slack.PlainTextType, "Close", false, false),
188179
Blocks: slack.Blocks{
189180
BlockSet: []slack.Block{
181+
slack.NewInputBlock(
182+
blockEnv,
183+
slack.NewTextBlockObject(slack.PlainTextType, "Environment", false, false),
184+
slack.NewOptionsSelectBlockElement(
185+
slack.OptTypeStatic,
186+
slack.NewTextBlockObject(slack.PlainTextType, "Select target environment", false, false),
187+
actionEnv,
188+
envs...,
189+
),
190+
),
191+
slack.NewInputBlock(
192+
blockType,
193+
slack.NewTextBlockObject(slack.PlainTextType, "Type", false, false),
194+
slack.NewOptionsSelectBlockElement(
195+
slack.OptTypeStatic,
196+
slack.NewTextBlockObject(slack.PlainTextType, "Select your ref type", false, false),
197+
actionType,
198+
slack.NewOptionBlockObject(
199+
"commit",
200+
slack.NewTextBlockObject(slack.PlainTextType, "Commit", false, false),
201+
nil,
202+
),
203+
slack.NewOptionBlockObject(
204+
"branch",
205+
slack.NewTextBlockObject(slack.PlainTextType, "Branch", false, false),
206+
nil,
207+
),
208+
slack.NewOptionBlockObject(
209+
"tag",
210+
slack.NewTextBlockObject(slack.PlainTextType, "Tag", false, false),
211+
nil,
212+
),
213+
),
214+
),
215+
slack.NewInputBlock(
216+
blockRef,
217+
slack.NewTextBlockObject(slack.PlainTextType, "Ref", false, false),
218+
slack.NewPlainTextInputBlockElement(
219+
slack.NewTextBlockObject(slack.PlainTextType, "E.g. Commit - 25a667d6, Branch - main, Tag - v0.1.2", false, false),
220+
actionRef,
221+
),
222+
),
190223
slack.InputBlock{
191-
Type: slack.MBTInput,
192-
BlockID: blockEnv,
193-
Label: &slack.TextBlockObject{
194-
Type: slack.PlainTextType,
195-
Text: "Environment",
196-
},
197-
Element: slack.SelectBlockElement{
198-
Type: slack.OptTypeStatic,
199-
ActionID: actionEnv,
200-
Placeholder: &slack.TextBlockObject{
201-
Type: slack.PlainTextType,
202-
Text: "Select target environment",
203-
},
204-
Options: envs,
205-
},
206-
},
207-
slack.InputBlock{
208-
Type: slack.MBTInput,
209-
BlockID: blockType,
210-
Label: &slack.TextBlockObject{
211-
Type: slack.PlainTextType,
212-
Text: "Reference Type",
213-
},
214-
Element: slack.SelectBlockElement{
215-
Type: slack.OptTypeStatic,
216-
ActionID: actionType,
217-
Placeholder: &slack.TextBlockObject{
218-
Type: slack.PlainTextType,
219-
Text: "Select your reference type",
220-
},
221-
Options: []*slack.OptionBlockObject{
222-
{
223-
Text: &slack.TextBlockObject{
224-
Type: slack.PlainTextType,
225-
Text: "Commit",
226-
},
227-
Value: "commit",
228-
},
229-
{
230-
Text: &slack.TextBlockObject{
231-
Type: slack.PlainTextType,
232-
Text: "Branch",
233-
},
234-
Value: "branch",
235-
},
236-
{
237-
Text: &slack.TextBlockObject{
238-
Type: slack.PlainTextType,
239-
Text: "Tag",
240-
},
241-
Value: "tag",
242-
},
243-
},
244-
},
245-
},
246-
slack.InputBlock{
247-
Type: slack.MBTInput,
248-
BlockID: blockRef,
249-
Label: &slack.TextBlockObject{
250-
Type: slack.PlainTextType,
251-
Text: "Reference",
252-
},
253-
Element: slack.PlainTextInputBlockElement{
254-
Type: slack.METPlainTextInput,
255-
ActionID: actionRef,
256-
Placeholder: &slack.TextBlockObject{
257-
Type: slack.PlainTextType,
258-
Text: "E.g. Commit - 25a667d6, Branch - main, Tag - v0.1.2",
259-
},
260-
},
261-
},
262-
slack.InputBlock{
263-
Type: slack.MBTInput,
264-
BlockID: blockApprovers,
265-
Label: &slack.TextBlockObject{
266-
Type: slack.PlainTextType,
267-
Text: "Approvers",
268-
},
224+
Type: slack.MBTInput,
225+
BlockID: blockApprovers,
226+
Label: slack.NewTextBlockObject(slack.PlainTextType, "Approvers", false, false),
269227
Optional: true,
270-
Element: slack.SelectBlockElement{
271-
Type: slack.MultiOptTypeStatic,
272-
ActionID: actionApprovers,
273-
Placeholder: &slack.TextBlockObject{
274-
Type: slack.PlainTextType,
275-
Text: "Select approvers",
276-
},
277-
Options: approvers,
278-
},
228+
Element: slack.NewOptionsSelectBlockElement(
229+
slack.MultiOptTypeStatic,
230+
slack.NewTextBlockObject(slack.PlainTextType, "Select approvers", false, false),
231+
actionApprovers,
232+
approvers...,
233+
),
279234
},
280235
},
281236
},

internal/server/slack/deploy_test.go

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,93 @@ import (
1010
"testing"
1111

1212
"github.com/gin-gonic/gin"
13+
"github.com/golang/mock/gomock"
14+
"github.com/slack-go/slack"
15+
"go.uber.org/zap"
16+
"gopkg.in/h2non/gock.v1"
17+
1318
"github.com/gitploy-io/gitploy/ent"
1419
"github.com/gitploy-io/gitploy/ent/deployment"
1520
"github.com/gitploy-io/gitploy/internal/server/slack/mock"
1621
"github.com/gitploy-io/gitploy/vo"
17-
"github.com/golang/mock/gomock"
18-
"go.uber.org/zap"
22+
)
23+
24+
const (
25+
pathPostMessage string = "chat.postMessage"
1926
)
2027

2128
func TestSlack_interactDeploy(t *testing.T) {
29+
t.Run("Post a message when the repository is locked.", func(t *testing.T) {
30+
m := mock.NewMockInteractor(gomock.NewController(t))
31+
32+
// These values are in "./testdata/deploy-interact.json"
33+
const (
34+
callbackID = "nafyVuEqzcchuVmV"
35+
chatUserID = "U025KUBB2"
36+
branch = "main"
37+
env = "prod"
38+
)
39+
40+
t.Log("Find the callback which was stored by the Slash command.")
41+
m.
42+
EXPECT().
43+
FindCallbackByHash(gomock.Any(), callbackID).
44+
Return(&ent.Callback{
45+
Edges: ent.CallbackEdges{
46+
Repo: &ent.Repo{
47+
ID: "1",
48+
Locked: true,
49+
},
50+
},
51+
}, nil)
52+
53+
t.Log("Find the chat-user who sent the payload.")
54+
m.
55+
EXPECT().
56+
FindChatUserByID(gomock.Any(), chatUserID).
57+
Return(&ent.ChatUser{}, nil)
58+
59+
t.Log("Get branch to validate the payload.")
60+
m.
61+
EXPECT().
62+
GetBranch(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{}), branch).
63+
Return(&vo.Branch{
64+
Name: branch,
65+
}, nil)
66+
67+
t.Log("Post a message when the repository is locked.")
68+
gock.
69+
New(slack.APIURL).
70+
Post(pathPostMessage).
71+
Reply(200)
72+
73+
s := &Slack{i: m, log: zap.L()}
74+
75+
gin.SetMode(gin.ReleaseMode)
76+
router := gin.New()
77+
router.POST("/interact", s.interactDeploy)
78+
79+
// Build the Slack payload.
80+
bytes, err := ioutil.ReadFile("./testdata/deploy-interact.json")
81+
if err != nil {
82+
t.Errorf("It has failed to open the JSON file: %s", err)
83+
t.FailNow()
84+
}
85+
86+
form := url.Values{}
87+
form.Add("payload", string(bytes))
88+
89+
req, _ := http.NewRequest("POST", "/interact", strings.NewReader(form.Encode()))
90+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
91+
92+
w := httptest.NewRecorder()
93+
router.ServeHTTP(w, req)
94+
95+
if w.Code != http.StatusOK {
96+
t.Fatalf("w.Code = %d, wanted %d. Body = %v", w.Code, http.StatusOK, w.Body)
97+
}
98+
})
99+
22100
t.Run("Create a new deployment with payload.", func(t *testing.T) {
23101
m := mock.NewMockInteractor(gomock.NewController(t))
24102

@@ -114,9 +192,7 @@ func TestSlack_interactDeploy(t *testing.T) {
114192
router.ServeHTTP(w, req)
115193

116194
if w.Code != http.StatusOK {
117-
t.Errorf("w.Code = %d, wanted %d", w.Code, http.StatusOK)
118-
t.Logf("w.Body = %v", w.Body)
119-
t.FailNow()
195+
t.Fatalf("w.Code = %d, wanted %d. Body = %v", w.Code, http.StatusOK, w.Body)
120196
}
121197
})
122198
}

0 commit comments

Comments
 (0)