Skip to content

Commit e4e812c

Browse files
authored
Merge pull request #77 from jiaozifs/test/auth_test
test: add test for cookie and jwt
2 parents 763b48a + be5983c commit e4e812c

File tree

9 files changed

+25
-12
lines changed

9 files changed

+25
-12
lines changed

controller/user_ctl.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ type UserController struct {
3333
}
3434

3535
func (userCtl UserController) Login(ctx context.Context, w *api.JiaozifsResponse, r *http.Request, body api.LoginJSONRequestBody) {
36-
3736
// get user encryptedPassword by username
3837
ep, err := userCtl.Repo.UserRepo().GetEPByName(ctx, body.Name)
3938
if err != nil {

integrationtest/branch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func BranchSpec(ctx context.Context, urlStr string) func(c convey.C) {
2323
prefix := "feat/"
2424

2525
createUser(ctx, c, client, userName)
26-
loginAndSwitch(ctx, c, client, userName)
26+
loginAndSwitch(ctx, c, client, "mike login", userName, false)
2727
createRepo(ctx, c, client, repoName)
2828

2929
c.Convey("create branch", func(c convey.C) {

integrationtest/commit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func GetEntriesInRefSpec(ctx context.Context, urlStr string) func(c convey.C) {
1919
branchName := "feat/get_entries_test"
2020

2121
createUser(ctx, c, client, userName)
22-
loginAndSwitch(ctx, c, client, userName)
22+
loginAndSwitch(ctx, c, client, "kitty login", userName, false)
2323
createRepo(ctx, c, client, repoName)
2424
createBranch(ctx, c, client, userName, repoName, "main", branchName)
2525
createWip(ctx, c, client, "feat get entries test0", userName, repoName, branchName)

integrationtest/helper_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,25 @@ func createUser(ctx context.Context, c convey.C, client *api.Client, userName st
114114
})
115115
}
116116

117-
func loginAndSwitch(ctx context.Context, c convey.C, client *api.Client, userName string) {
118-
c.Convey("login "+userName, func() {
117+
func loginAndSwitch(ctx context.Context, c convey.C, client *api.Client, title, userName string, useCookie bool) {
118+
c.Convey("login "+title, func() {
119119
resp, err := client.Login(ctx, api.LoginJSONRequestBody{
120120
Name: userName,
121121
Password: "12345678",
122122
})
123123
convey.So(err, convey.ShouldBeNil)
124124
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
125+
loginResult, err := api.ParseLoginResponse(resp)
126+
convey.So(err, convey.ShouldBeNil)
125127

126128
client.RequestEditors = nil
127129
client.RequestEditors = append(client.RequestEditors, func(ctx context.Context, req *http.Request) error {
128-
for _, cookie := range resp.Cookies() {
129-
req.AddCookie(cookie)
130+
if useCookie {
131+
for _, cookie := range resp.Cookies() {
132+
req.AddCookie(cookie)
133+
}
134+
} else {
135+
req.Header.Add("Authorization", "Bearer "+loginResult.JSON200.Token)
130136
}
131137
return nil
132138
})

integrationtest/objects_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func ObjectSpec(ctx context.Context, urlStr string) func(c convey.C) {
2222
branchName := "feat/obj_test"
2323

2424
createUser(ctx, c, client, userName)
25-
loginAndSwitch(ctx, c, client, userName)
25+
loginAndSwitch(ctx, c, client, "molly login", userName, false)
2626
createRepo(ctx, c, client, repoName)
2727
createBranch(ctx, c, client, userName, repoName, "main", branchName)
2828
createWip(ctx, c, client, "feat get obj test", userName, repoName, branchName)

integrationtest/repo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func RepoSpec(ctx context.Context, urlStr string) func(c convey.C) {
1919
userName := "jimmy"
2020
repoName := "happyrun"
2121
createUser(ctx, c, client, userName)
22-
loginAndSwitch(ctx, c, client, userName)
22+
loginAndSwitch(ctx, c, client, "jimmy login", userName, false)
2323

2424
c.Convey("create repo", func(c convey.C) {
2525
c.Convey("forbidden create repo name", func() {

integrationtest/user_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,20 @@ func UserSpec(ctx context.Context, urlStr string) func(c convey.C) {
3030
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusUnauthorized)
3131
})
3232

33-
loginAndSwitch(ctx, c, client, userName)
33+
loginAndSwitch(ctx, c, client, "admin login", userName, false)
3434

3535
c.Convey("usr profile", func() {
3636
resp, err := client.GetUserInfo(ctx)
3737
convey.So(err, convey.ShouldBeNil)
3838
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
3939
})
40+
41+
loginAndSwitch(ctx, c, client, "admin login again", userName, true)
42+
43+
c.Convey("usr profile with cookie", func() {
44+
resp, err := client.GetUserInfo(ctx)
45+
convey.So(err, convey.ShouldBeNil)
46+
convey.So(resp.StatusCode, convey.ShouldEqual, http.StatusOK)
47+
})
4048
}
4149
}

integrationtest/wip_object_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func WipObjectSpec(ctx context.Context, urlStr string) func(c convey.C) {
1818
branchName := "feat/wip_obj_test"
1919

2020
createUser(ctx, c, client, userName)
21-
loginAndSwitch(ctx, c, client, userName)
21+
loginAndSwitch(ctx, c, client, "jude login", userName, false)
2222
createRepo(ctx, c, client, repoName)
2323
createBranch(ctx, c, client, userName, repoName, "main", branchName)
2424
createWip(ctx, c, client, "get wip obj test", userName, repoName, branchName)

integrationtest/wip_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func WipSpec(ctx context.Context, urlStr string) func(c convey.C) {
1818
branchNameForDelete := "feat/wip_test2"
1919

2020
createUser(ctx, c, client, userName)
21-
loginAndSwitch(ctx, c, client, userName)
21+
loginAndSwitch(ctx, c, client, "july login", userName, false)
2222
createRepo(ctx, c, client, repoName)
2323
createBranch(ctx, c, client, userName, repoName, "main", branchName)
2424
createBranch(ctx, c, client, userName, repoName, "main", branchNameForDelete)

0 commit comments

Comments
 (0)