This repository was archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdeployment_change_test.go
98 lines (81 loc) · 2.14 KB
/
deployment_change_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
package repos
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/golang/mock/gomock"
"go.uber.org/zap"
"github.com/gitploy-io/gitploy/internal/server/api/v1/repos/mock"
"github.com/gitploy-io/gitploy/internal/server/global"
"github.com/gitploy-io/gitploy/model/ent"
"github.com/gitploy-io/gitploy/model/ent/deployment"
"github.com/gitploy-io/gitploy/model/extent"
)
func TestDeploymentAPI_ListChanges(t *testing.T) {
ctx := gomock.Any()
any := gomock.Any()
t.Run("Return commits successfully", func(t *testing.T) {
input := struct {
number int
page int
perPage int
}{
number: 5,
page: 1,
perPage: 30,
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := mock.NewMockInteractor(ctrl)
const (
base = "ee42de2"
head = "231eed1"
)
m.
EXPECT().
FindDeploymentOfRepoByNumber(ctx, any, gomock.Eq(input.number)).
Return(&ent.Deployment{
ID: 7,
Number: input.number,
Sha: head,
Status: deployment.StatusCreated,
}, nil)
m.
EXPECT().
CompareCommitsFromLastestDeployment(ctx, any, gomock.AssignableToTypeOf(&ent.Deployment{}), any).
Return([]*extent.Commit{
{
SHA: head,
},
}, nil)
// Ready the router to handle it.
gin.SetMode(gin.ReleaseMode)
s := DeploymentAPI{i: m, log: zap.L()}
router := gin.New()
router.GET("/deployments/:number/changes", func(c *gin.Context) {
// Mocking middlewares to return a user and a repository.
c.Set(global.KeyUser, &ent.User{})
c.Set(KeyRepo, &ent.Repo{})
}, s.ListChanges)
req, _ := http.NewRequest("GET", fmt.Sprintf("/deployments/%d/changes?page=%d&per_page=%d", input.number, input.page, input.perPage), nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("w.Code = %d, wanted %d", w.Code, http.StatusCreated)
t.FailNow()
}
expected := []*extent.Commit{
{
SHA: head,
},
}
eb, _ := json.Marshal(expected)
if bytes := w.Body.Bytes(); string(bytes) != string(eb) {
t.Errorf("w.Body = %s, wanted %s", string(bytes), string(eb))
t.FailNow()
}
})
}