woodpecker/remote/gitea/fixtures/handler.go

142 lines
3.1 KiB
Go
Raw Normal View History

2017-05-01 10:33:06 +00:00
package fixtures
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Handler returns an http.Handler that is capable of handling a variety of mock
2017-05-01 13:48:18 +00:00
// Gitea requests and returning mock responses.
2017-05-01 10:33:06 +00:00
func Handler() http.Handler {
gin.SetMode(gin.TestMode)
e := gin.New()
e.GET("/api/v1/repos/:owner/:name", getRepo)
e.GET("/api/v1/repos/:owner/:name/raw/:commit/:file", getRepoFile)
e.POST("/api/v1/repos/:owner/:name/hooks", createRepoHook)
e.GET("/api/v1/repos/:owner/:name/hooks", listRepoHooks)
e.DELETE("/api/v1/repos/:owner/:name/hooks/:id", deleteRepoHook)
2017-05-01 12:51:44 +00:00
e.POST("/api/v1/repos/:owner/:name/statuses/:commit", createRepoCommitStatus)
2017-05-01 10:33:06 +00:00
e.GET("/api/v1/user/repos", getUserRepos)
return e
}
func listRepoHooks(c *gin.Context) {
c.String(200, listRepoHookPayloads)
}
2017-05-01 10:33:06 +00:00
func getRepo(c *gin.Context) {
switch c.Param("name") {
case "repo_not_found":
c.String(404, "")
default:
c.String(200, repoPayload)
}
}
2017-05-01 12:51:44 +00:00
func createRepoCommitStatus(c *gin.Context) {
if c.Param("commit") == "v1.0.0" || c.Param("commit") == "9ecad50" {
c.String(200, repoPayload)
}
c.String(404, "")
}
2017-05-01 10:33:06 +00:00
func getRepoFile(c *gin.Context) {
if c.Param("file") == "file_not_found" {
c.String(404, "")
}
if c.Param("commit") == "v1.0.0" || c.Param("commit") == "9ecad50" {
c.String(200, repoFilePayload)
}
c.String(404, "")
}
func createRepoHook(c *gin.Context) {
in := struct {
Type string `json:"type"`
Conf struct {
Type string `json:"content_type"`
URL string `json:"url"`
} `json:"config"`
}{}
c.BindJSON(&in)
2017-05-01 14:43:23 +00:00
if in.Type != "gitea" ||
2017-05-01 10:33:06 +00:00
in.Conf.Type != "json" ||
in.Conf.URL != "http://localhost" {
c.String(500, "")
return
}
c.String(200, "{}")
}
func deleteRepoHook(c *gin.Context) {
c.String(200, "{}")
}
2017-05-01 10:33:06 +00:00
func getUserRepos(c *gin.Context) {
switch c.Request.Header.Get("Authorization") {
case "token repos_not_found":
c.String(404, "")
default:
c.String(200, userRepoPayload)
}
}
const listRepoHookPayloads = `
[
{
"id": 1,
"type": "gitea",
"config": {
"content_type": "json",
"url": "http:\/\/localhost\/hook?access_token=1234567890"
}
}
]
`
2017-05-01 10:33:06 +00:00
const repoPayload = `
{
"owner": {
2017-05-01 12:51:44 +00:00
"login": "test_name",
2017-05-01 10:33:06 +00:00
"email": "octocat@github.com",
"avatar_url": "https:\/\/secure.gravatar.com\/avatar\/8c58a0be77ee441bb8f8595b7f1b4e87"
},
"full_name": "test_name\/repo_name",
"private": true,
"html_url": "http:\/\/localhost\/test_name\/repo_name",
"clone_url": "http:\/\/localhost\/test_name\/repo_name.git",
"permissions": {
"admin": true,
"push": true,
"pull": true
}
}
`
const repoFilePayload = `{ platform: linux/amd64 }`
const userRepoPayload = `
[
{
"owner": {
2017-05-01 12:51:44 +00:00
"login": "test_name",
2017-05-01 10:33:06 +00:00
"email": "octocat@github.com",
"avatar_url": "https:\/\/secure.gravatar.com\/avatar\/8c58a0be77ee441bb8f8595b7f1b4e87"
},
"full_name": "test_name\/repo_name",
"private": true,
"html_url": "http:\/\/localhost\/test_name\/repo_name",
"clone_url": "http:\/\/localhost\/test_name\/repo_name.git",
"permissions": {
"admin": true,
"push": true,
"pull": true
}
}
]
`