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) {
|
2024-01-10 14:34:44 +00:00
|
|
|
if c.PostForm("error") == "invalid_scope" {
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusInternalServerError, "")
|
2016-12-19 05:42:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
switch c.PostForm("code") {
|
|
|
|
case "code_bad_request":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusInternalServerError, "")
|
2016-05-01 06:22:30 +00:00
|
|
|
return
|
|
|
|
case "code_user_not_found":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, tokenNotFoundPayload)
|
2016-05-01 06:22:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
switch c.PostForm("refresh_token") {
|
|
|
|
case "refresh_token_not_found":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2016-05-01 06:22:30 +00:00
|
|
|
case "refresh_token_is_empty":
|
|
|
|
c.Header("Content-Type", "application/json")
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, "{}")
|
2016-05-01 06:22:30 +00:00
|
|
|
default:
|
|
|
|
c.Header("Content-Type", "application/json")
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, tokenPayload)
|
2016-05-01 06:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2022-09-18 07:26:13 +00:00
|
|
|
default:
|
2023-04-30 01:40:13 +00:00
|
|
|
if c.Query("page") == "" || c.Query("page") == "1" {
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, workspacesPayload)
|
2023-04-30 01:40:13 +00:00
|
|
|
} else {
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, "{\"values\":[]}")
|
2023-04-30 01:40:13 +00:00
|
|
|
}
|
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":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2018-04-27 17:48:54 +00:00
|
|
|
case "permission_read", "permission_write", "permission_admin":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, fmt.Sprintf(permissionRepoPayload, c.Param("name")))
|
2016-04-30 08:00:39 +00:00
|
|
|
default:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, repoPayload)
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRepoHooks(c *gin.Context) {
|
|
|
|
switch c.Param("name") {
|
|
|
|
case "hooks_not_found", "repo_no_hooks":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2016-04-30 08:00:39 +00:00
|
|
|
case "hook_empty":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, "{}")
|
2016-04-30 08:00:39 +00:00
|
|
|
default:
|
2023-10-23 16:44:25 +00:00
|
|
|
if c.Query("page") == "" || c.Query("page") == "1" {
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, repoHookPayload)
|
2023-10-23 16:44:25 +00:00
|
|
|
} else {
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, "{\"values\":[]}")
|
2023-10-23 16:44:25 +00:00
|
|
|
}
|
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":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, repoDirPayload)
|
2024-05-01 10:22:07 +00:00
|
|
|
case "dir_not_found":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2016-04-30 08:00:39 +00:00
|
|
|
case "file_not_found":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2016-04-30 08:00:39 +00:00
|
|
|
default:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, repoFilePayload)
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 00:06:03 +00:00
|
|
|
func getBranchHead(c *gin.Context) {
|
|
|
|
switch c.Param("commit") {
|
|
|
|
case "branch_name":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, branchCommitsPayload)
|
2023-08-03 00:06:03 +00:00
|
|
|
default:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2023-08-03 00:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPullRequests(c *gin.Context) {
|
|
|
|
switch c.Param("name") {
|
|
|
|
case "repo_name":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, pullRequestsPayload)
|
2023-08-03 00:06:03 +00:00
|
|
|
default:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2023-08-03 00:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
func createRepoStatus(c *gin.Context) {
|
|
|
|
switch c.Param("name") {
|
|
|
|
case "repo_not_found":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2016-04-30 08:00:39 +00:00
|
|
|
default:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, "")
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRepoHook(c *gin.Context) {
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, "")
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteRepoHook(c *gin.Context) {
|
|
|
|
switch c.Param("name") {
|
|
|
|
case "hook_not_found":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2016-04-30 08:00:39 +00:00
|
|
|
default:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, "")
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUser(c *gin.Context) {
|
|
|
|
switch c.Request.Header.Get("Authorization") {
|
|
|
|
case "Bearer user_not_found", "Bearer a87ff679":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2016-04-30 08:00:39 +00:00
|
|
|
default:
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, userPayload)
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserRepos(c *gin.Context) {
|
|
|
|
switch c.Request.Header.Get("Authorization") {
|
|
|
|
case "Bearer repos_not_found", "Bearer 70efdf2e":
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusNotFound, "")
|
2016-04-30 08:00:39 +00:00
|
|
|
default:
|
2023-10-23 16:44:25 +00:00
|
|
|
if c.Query("page") == "" || c.Query("page") == "1" {
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, userRepoPayload)
|
2023-10-23 16:44:25 +00:00
|
|
|
} else {
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, "{\"values\":[]}")
|
2023-10-23 16:44:25 +00:00
|
|
|
}
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 17:48:54 +00:00
|
|
|
func getPermissions(c *gin.Context) {
|
2024-01-19 03:15:47 +00:00
|
|
|
if c.Query("page") == "" || c.Query("page") == "1" {
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, permissionsPayLoad)
|
2024-01-19 03:15:47 +00:00
|
|
|
} else {
|
2024-03-15 17:00:25 +00:00
|
|
|
c.String(http.StatusOK, "{\"values\":[]}")
|
2018-04-27 17:48:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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": [
|
|
|
|
{
|
2024-02-11 09:44:50 +00:00
|
|
|
"hash": "branch_head_name",
|
|
|
|
"links": {
|
|
|
|
"html": {
|
|
|
|
"href": "https://bitbucket.org/commitlink"
|
|
|
|
}
|
|
|
|
}
|
2023-08-03 00:06:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"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 = `
|
|
|
|
{
|
2024-01-23 15:35:30 +00:00
|
|
|
"uuid": "{4d8c0f46-cd62-4b77-b0cf-faa3e4d932c6}",
|
2016-04-30 08:00:39 +00:00
|
|
|
"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
|
|
|
|
2024-01-19 03:15:47 +00:00
|
|
|
const permissionsPayLoad = `
|
2018-04-27 17:48:54 +00:00
|
|
|
{
|
2024-01-19 03:15:47 +00:00
|
|
|
"pagelen": 100,
|
|
|
|
"page": 1,
|
2018-04-27 17:48:54 +00:00
|
|
|
"values": [
|
|
|
|
{
|
2024-01-19 03:15:47 +00:00
|
|
|
"repository": {
|
|
|
|
"full_name": "test_name/repo_name"
|
|
|
|
},
|
|
|
|
"permission": "read"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"repository": {
|
|
|
|
"full_name": "test_name/permission_read"
|
|
|
|
},
|
|
|
|
"permission": "read"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"repository": {
|
|
|
|
"full_name": "test_name/permission_write"
|
|
|
|
},
|
|
|
|
"permission": "write"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"repository": {
|
|
|
|
"full_name": "test_name/permission_admin"
|
|
|
|
},
|
|
|
|
"permission": "admin"
|
2018-04-27 17:48:54 +00:00
|
|
|
}
|
2024-01-19 03:15:47 +00:00
|
|
|
]
|
2018-04-27 17:48:54 +00:00
|
|
|
}
|
|
|
|
`
|