Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some tests for bitbucket forge #2097

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions server/forge/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,36 @@ func Test_bitbucket(t *testing.T) {
})
})

g.Describe("When requesting repo branch HEAD", func() {
g.It("Should return the details", func() {
branchHead, err := c.BranchHead(ctx, fakeUser, fakeRepo, "branch_name")
g.Assert(err).IsNil()
g.Assert(branchHead).Equal("branch_head_name")
})
g.It("Should handle not found errors", func() {
_, err := c.BranchHead(ctx, fakeUser, fakeRepo, "branch_not_found")
g.Assert(err).IsNotNil()
})
})

g.Describe("When requesting repo pull requests", func() {
listOpts := model.ListOptions{
All: false,
Page: 1,
PerPage: 10,
}
g.It("Should return the details", func() {
repoPRs, err := c.PullRequests(ctx, fakeUser, fakeRepo, &listOpts)
g.Assert(err).IsNil()
g.Assert(repoPRs[0].Title).Equal("PRs title")
g.Assert(repoPRs[0].Index).Equal(int64(123))
})
g.It("Should handle not found errors", func() {
_, err := c.PullRequests(ctx, fakeUser, fakeRepoNotFound, &listOpts)
g.Assert(err).IsNotNil()
})
})

g.Describe("When requesting repo directory contents", func() {
g.It("Should return the details", func() {
files, err := c.Dir(ctx, fakeUser, fakeRepo, fakePipeline, "/dir")
Expand Down
55 changes: 54 additions & 1 deletion server/forge/bitbucket/fixtures/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func Handler() http.Handler {
e.GET("/2.0/repositories/:owner", getUserRepos)
e.GET("/2.0/user/", getUser)
e.GET("/2.0/user/permissions/repositories", getPermissions)

e.GET("/2.0/repositories/:owner/:name/commits/:commit", getBranchHead)
e.GET("/2.0/repositories/:owner/:name/pullrequests", getPullRequests)
return e
}

Expand Down Expand Up @@ -119,6 +120,24 @@ func getRepoFile(c *gin.Context) {
}
}

func getBranchHead(c *gin.Context) {
switch c.Param("commit") {
case "branch_name":
c.String(200, branchCommitsPayload)
default:
c.String(404, "")
}
}

func getPullRequests(c *gin.Context) {
switch c.Param("name") {
case "repo_name":
c.String(200, pullRequestsPayload)
default:
c.String(404, "")
}
}

func createRepoStatus(c *gin.Context) {
switch c.Param("name") {
case "repo_not_found":
Expand Down Expand Up @@ -248,6 +267,40 @@ const repoDirPayload = `
}
`

const branchCommitsPayload = `
{
"values": [
{
"hash": "branch_head_name"
},
{
"hash": "random1"
},
{
"hash": "random2"
}
]
}
`

const pullRequestsPayload = `
{
"values": [
{
"id": 123,
"title": "PRs title"
},
{
"id": 456,
"title": "Another PRs title"
}
],
"pagelen": 10,
"size": 2,
"page": 1
}
`

const userPayload = `
{
"username": "superman",
Expand Down