Skip to content

Commit

Permalink
refactor: update mock to live in server (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neal authored Oct 29, 2021
1 parent fe90645 commit 4e64f89
Show file tree
Hide file tree
Showing 14 changed files with 2,147 additions and 0 deletions.
75 changes: 75 additions & 0 deletions mock/server/authentication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package server

import (
"encoding/json"
"net/http"

"github.com/gin-gonic/gin"
"github.com/go-vela/types"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
)

const (
// TokenRefreshResp represents a JSON return for a token refresh.
// nolint:gosec // not a hardcoded credential
TokenRefreshResp = `{
"token": "header.payload.signature"
}`
)

// getTokenRefresh returns mock JSON for a http GET.
func getTokenRefresh(c *gin.Context) {
data := []byte(TokenRefreshResp)

var body library.Login
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusOK, body)
}

// getAuthenticate returns mock response for a http GET.
//
// Don't pass "state" and "code" params to receive an error response.
func getAuthenticate(c *gin.Context) {
data := []byte(TokenRefreshResp)

state := c.Request.FormValue("state")
code := c.Request.FormValue("code")
err := "error"

if len(state) == 0 && len(code) == 0 {
c.AbortWithStatusJSON(http.StatusUnauthorized, types.Error{Message: &err})

return
}

var body library.Login
_ = json.Unmarshal(data, &body)

c.SetCookie(constants.RefreshTokenName, "refresh", 2, "/", "", true, true)

c.JSON(http.StatusOK, body)
}

// getAuthenticateFromToken returns mock response for a http POST.
//
// Don't pass "Token" in header to receive an error message.
func getAuthenticateFromToken(c *gin.Context) {
data := []byte(TokenRefreshResp)
err := "error"

token := c.Request.Header.Get("Token")
if len(token) == 0 {
c.AbortWithStatusJSON(http.StatusUnauthorized, types.Error{Message: &err})
}

var body library.Login
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusOK, body)
}
307 changes: 307 additions & 0 deletions mock/server/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package server

import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/go-vela/types"
"github.com/go-vela/types/library"
)

const (
// BuildResp represents a JSON return for a single build.
BuildResp = `{
"id": 1,
"repo_id": 1,
"number": 1,
"parent": 1,
"event": "push",
"status": "created",
"error": "",
"enqueued": 1563474077,
"created": 1563474076,
"started": 1563474077,
"finished": 0,
"deploy": "",
"clone": "https://github.com/github/octocat.git",
"source": "https://github.com/github/octocat/commit/48afb5bdc41ad69bf22588491333f7cf71135163",
"title": "push received from https://github.com/github/octocat",
"message": "First commit...",
"commit": "48afb5bdc41ad69bf22588491333f7cf71135163",
"sender": "OctoKitty",
"author": "OctoKitty",
"email": "[email protected]",
"link": "https://vela.example.company.com/github/octocat/1",
"branch": "master",
"ref": "refs/heads/master",
"base_ref": "",
"host": "example.company.com",
"runtime": "docker",
"distribution": "linux"
}`

// BuildsResp represents a JSON return for one to many builds.
BuildsResp = `[
{
"id": 2,
"repo_id": 1,
"number": 2,
"parent": 1,
"event": "push",
"status": "running",
"error": "",
"enqueued": 1563474204,
"created": 1563474204,
"started": 1563474204,
"finished": 0,
"deploy": "",
"clone": "https://github.com/github/octocat.git",
"source": "https://github.com/github/octocat/commit/48afb5bdc41ad69bf22588491333f7cf71135163",
"title": "push received from https://github.com/github/octocat",
"message": "Second commit...",
"commit": "48afb5bdc41ad69bf22588491333f7cf71135163",
"sender": "OctoKitty",
"author": "OctoKitty",
"email": "[email protected]",
"link": "https://vela.example.company.com/github/octocat/1",
"branch": "master",
"ref": "refs/heads/master",
"base_ref": "",
"host": "ed95dcc0687c",
"runtime": "",
"distribution": ""
},
{
"id": 1,
"repo_id": 1,
"number": 1,
"parent": 1,
"event": "push",
"status": "running",
"error": "",
"enqueued": 1563474077,
"created": 1563474076,
"started": 1563474077,
"finished": 0,
"deploy": "",
"clone": "https://github.com/github/octocat.git",
"source": "https://github.com/github/octocat/commit/48afb5bdc41ad69bf22588491333f7cf71135163",
"title": "push received from https://github.com/github/octocat",
"message": "First commit...",
"commit": "48afb5bdc41ad69bf22588491333f7cf71135163",
"sender": "OctoKitty",
"author": "OctoKitty",
"email": "[email protected]",
"link": "https://vela.example.company.com/github/octocat/1",
"branch": "master",
"ref": "refs/heads/master",
"base_ref": "",
"host": "82823eb770b0",
"runtime": "",
"distribution": ""
}
]`

// BuildLogsResp represents a JSON return for build logs.
BuildLogsResp = `[
{
"id": 1,
"step_id": 1,
"build_id": 1,
"repo_id": 1,
"data": "SGVsbG8sIFdvcmxkIQ=="
},
{
"id": 2,
"step_id": 2,
"build_id": 1,
"repo_id": 1,
"data": "SGVsbG8sIFdvcmxkIQ=="
}
]`

// BuildQueueResp represents a JSON return for build queue.
BuildQueueResp = `[
{
"status": "running",
"created": 1616467142,
"number": 6,
"full_name": "github/octocat"
},
{
"status": "pending",
"created": 1616467142,
"number": 7,
"full_name": "github/octocat"
}
]`
)

// getBuilds returns mock JSON for a http GET.
func getBuilds(c *gin.Context) {
data := []byte(BuildsResp)

var body []library.Build
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusOK, body)
}

// getBuild has a param :build returns mock JSON for a http GET.
func getBuild(c *gin.Context) {
b := c.Param("build")

if strings.EqualFold(b, "0") {
msg := fmt.Sprintf("Build %s does not exist", b)

c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})

return
}

data := []byte(BuildResp)

var body library.Build
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusOK, body)
}

// getLogs has a param :build returns mock JSON for a http GET.
//
// Pass "0" to :build to test receiving a http 404 response.
func getLogs(c *gin.Context) {
b := c.Param("build")

if strings.EqualFold(b, "0") {
msg := fmt.Sprintf("Build %s does not exist", b)

c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})

return
}

data := []byte(BuildLogsResp)

var body []library.Log
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusOK, body)
}

// addBuild returns mock JSON for a http POST.
func addBuild(c *gin.Context) {
data := []byte(BuildResp)

var body library.Build
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusCreated, body)
}

// updateBuild has a param :build returns mock JSON for a http PUT.
//
// Pass "0" to :build to test receiving a http 404 response.
func updateBuild(c *gin.Context) {
if !strings.Contains(c.FullPath(), "admin") {
b := c.Param("build")

if strings.EqualFold(b, "0") {
msg := fmt.Sprintf("Build %s does not exist", b)

c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})

return
}
}

data := []byte(BuildResp)

var body library.Build
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusOK, body)
}

// removeBuild has a param :build returns mock JSON for a http DELETE.
//
// Pass "0" to :build to test receiving a http 404 response.
func removeBuild(c *gin.Context) {
b := c.Param("build")

if strings.EqualFold(b, "0") {
msg := fmt.Sprintf("Build %s does not exist", b)

c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})

return
}

c.JSON(http.StatusOK, fmt.Sprintf("Build %s removed", b))
}

// restartBuild has a param :build returns mock JSON for a http POST.
//
// Pass "0" to :build to test receiving a http 404 response.
func restartBuild(c *gin.Context) {
b := c.Param("build")

if strings.EqualFold(b, "0") {
msg := fmt.Sprintf("Build %s does not exist", b)

c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})

return
}

data := []byte(BuildResp)

var body library.Build
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusCreated, body)
}

// cancelBuild has a param :build returns mock JSON for a http DELETE.
//
// Pass "0" to :build to test receiving a http 404 response.
func cancelBuild(c *gin.Context) {
b := c.Param("build")

if strings.EqualFold(b, "0") {
msg := fmt.Sprintf("Build %s does not exist", b)

c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})

return
}

c.JSON(http.StatusOK, BuildResp)
}

// buildQueue has a param :after returns mock JSON for a http GET.
//
// Pass "0" to :after to test receiving a http 200 response with no builds.
func buildQueue(c *gin.Context) {
b := c.Param("after")

if strings.EqualFold(b, "0") {
c.AbortWithStatusJSON(http.StatusOK, []string{})

return
}

data := []byte(BuildQueueResp)

var body []library.BuildQueue
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusOK, body)
}
Loading

0 comments on commit 4e64f89

Please sign in to comment.