2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
package fixtures
|
|
|
|
|
|
|
|
import (
|
2018-04-27 17:48:54 +00:00
|
|
|
"fmt"
|
2016-04-30 08:00:39 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler returns an http.Handler that is capable of handling a variety of mock
|
|
|
|
// Bitbucket requests and returning mock responses.
|
|
|
|
func Handler() http.Handler {
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
|
|
|
e := gin.New()
|
2016-05-01 06:22:30 +00:00
|
|
|
e.POST("/site/oauth2/access_token", getOauth)
|
2022-09-18 07:26:13 +00:00
|
|
|
e.GET("/2.0/workspaces/", getWorkspaces)
|
2016-04-30 08:00:39 +00:00
|
|
|
e.GET("/2.0/repositories/:owner/:name", getRepo)
|
|
|
|
e.GET("/2.0/repositories/:owner/:name/hooks", getRepoHooks)
|
2019-06-13 14:33:51 +00:00
|
|
|
e.GET("/2.0/repositories/:owner/:name/src/:commit/:file", getRepoFile)
|
2016-04-30 08:00:39 +00:00
|
|
|
e.DELETE("/2.0/repositories/:owner/:name/hooks/:hook", deleteRepoHook)
|
|
|
|
e.POST("/2.0/repositories/:owner/:name/hooks", createRepoHook)
|
|
|
|
e.POST("/2.0/repositories/:owner/:name/commit/:commit/statuses/build", createRepoStatus)
|
|
|
|
e.GET("/2.0/repositories/:owner", getUserRepos)
|
|
|
|
e.GET("/2.0/user/", getUser)
|
2018-04-27 17:48:54 +00:00
|
|
|
e.GET("/2.0/user/permissions/repositories", getPermissions)
|
2023-08-03 00:06:03 +00:00
|
|
|
e.GET("/2.0/repositories/:owner/:name/commits/:commit", getBranchHead)
|
|
|
|
e.GET("/2.0/repositories/:owner/:name/pullrequests", getPullRequests)
|
2016-04-30 08:00:39 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
func getOauth(c *gin.Context) {
|
2016-12-19 05:42:56 +00:00
|
|
|
switch c.PostForm("error") {
|
|
|
|
case "invalid_scope":
|
|
|
|
c.String(500, "")
|
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
switch c.PostForm("code") {
|
|
|
|
case "code_bad_request":
|
|
|
|
c.String(500, "")
|
|
|
|
return
|
|
|
|
case "code_user_not_found":
|
|
|
|
c.String(200, tokenNotFoundPayload)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch c.PostForm("refresh_token") {
|
|
|
|
case "refresh_token_not_found":
|
|
|
|
c.String(404, "")
|
|
|
|
case "refresh_token_is_empty":
|
|
|
|
c.Header("Content-Type", "application/json")
|
|
|
|
c.String(200, "{}")
|
|
|
|
default:
|
|
|
|
c.Header("Content-Type", "application/json")
|
|
|
|
c.String(200, tokenPayload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 07:26:13 +00:00
|
|
|
func getWorkspaces(c *gin.Context) {
|
|
|
|
// TODO: should the role be used ?
|
|
|
|
// role, _ := c.Params.Get("role")
|
|
|
|
|
|
|
|
switch c.Request.Header.Get("Authorization") {
|
|
|
|
case "Bearer teams_not_found", "Bearer c81e728d":
|
|
|
|
c.String(404, "")
|
|
|
|
default:
|
2023-04-30 01:40:13 +00:00
|
|
|
if c.Query("page") == "" || c.Query("page") == "1" {
|
|
|
|
c.String(200, workspacesPayload)
|
|
|
|
} else {
|
|
|
|
c.String(200, "{\"values\":[]}")
|
|
|
|
}
|
2022-09-18 07:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
func getRepo(c *gin.Context) {
|
|
|
|
switch c.Param("name") {
|
|
|
|
case "not_found", "repo_unknown", "repo_not_found":
|
|
|
|
c.String(404, "")
|
2018-04-27 17:48:54 +00:00
|
|
|
case "permission_read", "permission_write", "permission_admin":
|
|
|
|
c.String(200, fmt.Sprintf(permissionRepoPayload, c.Param("name")))
|
2016-04-30 08:00:39 +00:00
|
|
|
default:
|
|
|
|
c.String(200, repoPayload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRepoHooks(c *gin.Context) {
|
|
|
|
switch c.Param("name") {
|
|
|
|
case "hooks_not_found", "repo_no_hooks":
|
|
|
|
c.String(404, "")
|
|
|
|
case "hook_empty":
|
|
|
|
c.String(200, "{}")
|
|
|
|
default:
|
2023-10-23 16:44:25 +00:00
|
|
|
if c.Query("page") == "" || c.Query("page") == "1" {
|
|
|
|
c.String(200, repoHookPayload)
|
|
|
|
} else {
|
|
|
|
c.String(200, "{\"values\":[]}")
|
|
|
|
}
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRepoFile(c *gin.Context) {
|
|
|
|
switch c.Param("file") {
|
2023-08-02 11:15:57 +00:00
|
|
|
case "dir":
|
|
|
|
c.String(200, repoDirPayload)
|
|
|
|
case "dir_not_found/":
|
|
|
|
c.String(404, "")
|
2016-04-30 08:00:39 +00:00
|
|
|
case "file_not_found":
|
|
|
|
c.String(404, "")
|
|
|
|
default:
|
|
|
|
c.String(200, repoFilePayload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 00:06:03 +00:00
|
|
|
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, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
func createRepoStatus(c *gin.Context) {
|
|
|
|
switch c.Param("name") {
|
|
|
|
case "repo_not_found":
|
|
|
|
c.String(404, "")
|
|
|
|
default:
|
|
|
|
c.String(200, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRepoHook(c *gin.Context) {
|
|
|
|
c.String(200, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteRepoHook(c *gin.Context) {
|
|
|
|
switch c.Param("name") {
|
|
|
|
case "hook_not_found":
|
|
|
|
c.String(404, "")
|
|
|
|
default:
|
|
|
|
c.String(200, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUser(c *gin.Context) {
|
|
|
|
switch c.Request.Header.Get("Authorization") {
|
|
|
|
case "Bearer user_not_found", "Bearer a87ff679":
|
|
|
|
c.String(404, "")
|
|
|
|
default:
|
|
|
|
c.String(200, userPayload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserRepos(c *gin.Context) {
|
|
|
|
switch c.Request.Header.Get("Authorization") {
|
|
|
|
case "Bearer repos_not_found", "Bearer 70efdf2e":
|
|
|
|
c.String(404, "")
|
|
|
|
default:
|
2023-10-23 16:44:25 +00:00
|
|
|
if c.Query("page") == "" || c.Query("page") == "1" {
|
|
|
|
c.String(200, userRepoPayload)
|
|
|
|
} else {
|
|
|
|
c.String(200, "{\"values\":[]}")
|
|
|
|
}
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 17:48:54 +00:00
|
|
|
func permission(p string) string {
|
|
|
|
return fmt.Sprintf(permissionPayload, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPermissions(c *gin.Context) {
|
|
|
|
query := c.Request.URL.Query()["q"][0]
|
|
|
|
switch query {
|
|
|
|
case `repository.full_name="test_name/permission_read"`:
|
|
|
|
c.String(200, permission("read"))
|
|
|
|
case `repository.full_name="test_name/permission_write"`:
|
|
|
|
c.String(200, permission("write"))
|
|
|
|
case `repository.full_name="test_name/permission_admin"`:
|
|
|
|
c.String(200, permission("admin"))
|
|
|
|
default:
|
|
|
|
c.String(200, permission("read"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
const tokenPayload = `
|
|
|
|
{
|
|
|
|
"access_token":"2YotnFZFEjr1zCsicMWpAA",
|
|
|
|
"refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
|
|
|
|
"token_type":"Bearer",
|
|
|
|
"expires_in":3600
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const tokenNotFoundPayload = `
|
|
|
|
{
|
|
|
|
"access_token":"user_not_found",
|
|
|
|
"refresh_token":"user_not_found",
|
|
|
|
"token_type":"Bearer",
|
|
|
|
"expires_in":3600
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
const repoPayload = `
|
|
|
|
{
|
|
|
|
"full_name": "test_name/repo_name",
|
|
|
|
"scm": "git",
|
|
|
|
"is_private": true
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2018-04-27 17:48:54 +00:00
|
|
|
const permissionRepoPayload = `
|
|
|
|
{
|
|
|
|
"full_name": "test_name/%s",
|
|
|
|
"scm": "git",
|
|
|
|
"is_private": true
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
const repoHookPayload = `
|
|
|
|
{
|
|
|
|
"pagelen": 10,
|
|
|
|
"values": [
|
|
|
|
{
|
|
|
|
"uuid": "{afe61e14-2c5f-49e8-8b68-ad1fb55fc052}",
|
|
|
|
"url": "http://127.0.0.1"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"page": 1,
|
|
|
|
"size": 1
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2019-06-13 14:33:51 +00:00
|
|
|
const repoFilePayload = "dummy payload"
|
2016-04-30 08:00:39 +00:00
|
|
|
|
2023-08-02 11:15:57 +00:00
|
|
|
const repoDirPayload = `
|
|
|
|
{
|
|
|
|
"pagelen": 10,
|
|
|
|
"page": 1,
|
|
|
|
"values": [
|
|
|
|
{
|
|
|
|
"path": "README.md",
|
|
|
|
"type": "commit_file"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"path": "test",
|
|
|
|
"type": "commit_directory"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"path": ".gitignore",
|
|
|
|
"type": "commit_file"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2023-08-03 00:06:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
const userPayload = `
|
|
|
|
{
|
|
|
|
"username": "superman",
|
|
|
|
"links": {
|
|
|
|
"avatar": {
|
|
|
|
"href": "http:\/\/i.imgur.com\/ZygP55A.jpg"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"type": "user"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const userRepoPayload = `
|
|
|
|
{
|
|
|
|
"page": 1,
|
|
|
|
"pagelen": 10,
|
|
|
|
"size": 1,
|
|
|
|
"values": [
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"avatar": {
|
|
|
|
"href": "http:\/\/i.imgur.com\/ZygP55A.jpg"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"full_name": "test_name/repo_name",
|
|
|
|
"scm": "git",
|
|
|
|
"is_private": true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2022-09-18 07:26:13 +00:00
|
|
|
const workspacesPayload = `
|
2016-04-30 08:00:39 +00:00
|
|
|
{
|
2022-09-18 07:26:13 +00:00
|
|
|
"page": 1,
|
2016-04-30 08:00:39 +00:00
|
|
|
"pagelen": 100,
|
2022-09-18 07:26:13 +00:00
|
|
|
"size": 1,
|
2016-04-30 08:00:39 +00:00
|
|
|
"values": [
|
|
|
|
{
|
2022-09-18 07:26:13 +00:00
|
|
|
"type": "workspace",
|
|
|
|
"uuid": "{c7a04a76-fa20-43e4-dc42-a7506db4c95b}",
|
|
|
|
"name": "Ueber Dev",
|
|
|
|
"slug": "ueberdev42",
|
2016-04-30 08:00:39 +00:00
|
|
|
"links": {
|
2022-09-18 07:26:13 +00:00
|
|
|
"avatar": {
|
|
|
|
"href": "https://bitbucket.org/workspaces/ueberdev42/avatar/?ts=1658761964"
|
|
|
|
},
|
|
|
|
"html": {
|
|
|
|
"href": "https://bitbucket.org/ueberdev42/"
|
|
|
|
},
|
|
|
|
"self": {
|
|
|
|
"href": "https://api.bitbucket.org/2.0/workspaces/ueberdev42"
|
|
|
|
}
|
|
|
|
}
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
2018-04-27 17:48:54 +00:00
|
|
|
|
|
|
|
const permissionPayload = `
|
|
|
|
{
|
|
|
|
"pagelen": 1,
|
|
|
|
"values": [
|
|
|
|
{
|
|
|
|
"permission": "%s"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"page": 1
|
|
|
|
}
|
|
|
|
`
|