Fixing integration tests

This commit is contained in:
George Fleury
2023-08-30 12:21:48 +02:00
parent 12fd8cfe8b
commit 5d0b8f4baf
14 changed files with 391 additions and 235 deletions

View File

@ -10,7 +10,12 @@
package swagger
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"github.com/gorilla/mux"
)
func AddGroupToUser(w http.ResponseWriter, r *http.Request) {
@ -54,6 +59,27 @@ func CountPullRequestTasks(w http.ResponseWriter, r *http.Request) {
}
func Create(w http.ResponseWriter, r *http.Request) {
repositorySlug := mux.Vars(r)["repositorySlug"]
if strings.Compare(repositorySlug, "repo1_test1") == 0 {
HandleError(w, http.StatusBadRequest, map[string]interface{}{"errors": []interface{}{
map[string]interface{}{
"context": nil,
"message": "title must be supplied for this request",
"exceptionName": nil,
},
}})
return
} else if strings.Compare(repositorySlug, "repo1_test2") == 0 {
HandleError(w, http.StatusNotFound, map[string]interface{}{"errors": []interface{}{
map[string]interface{}{
"context": nil,
"message": "Repository \"repo1\" of project with key \"PROJ\" has no branch \"refs/heads/feature\"",
"exceptionName": "com.atlassian.bitbucket.commit.NoSuchCommitException",
},
}})
return
}
HandleRequest(w, r)
}
@ -62,10 +88,37 @@ func CreateBranch(w http.ResponseWriter, r *http.Request) {
}
func CreateComment(w http.ResponseWriter, r *http.Request) {
commit_id := mux.Vars(r)["commitId"]
if strings.Compare(commit_id, "657f55ce41710f9bfde15c374837136728fae9d9e0eca0b97cb7bfea5095af30") == 0 {
HandleError(w, http.StatusNotFound, map[string]interface{}{"errors": []interface{}{
map[string]interface{}{
"context": nil,
"message": "Commit '657f55ce41710f9bfde15c374837136728fae9d9e0eca0b97cb7bfea5095af30' does not exist in repository 'repo1'.",
"exceptionName": "com.atlassian.bitbucket.commit.NoSuchCommitException",
},
}})
return
}
HandleRequest(w, r)
}
func CreateCommentCommit(w http.ResponseWriter, r *http.Request) {
pr_id := mux.Vars(r)["pullRequestId"]
if id, err := strconv.Atoi(pr_id); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
} else if id == 1 {
HandleError(w, http.StatusNotFound, map[string]interface{}{"errors": []interface{}{
map[string]interface{}{
"context": nil,
"message": "Pull request 1 does not exist in PROJ/repo1.",
"exceptionName": "com.atlassian.bitbucket.pull.NoSuchPullRequestException",
},
}})
return
}
HandleRequest(w, r)
}
@ -102,9 +155,32 @@ func Decline(w http.ResponseWriter, r *http.Request) {
}
func Delete(w http.ResponseWriter, r *http.Request) {
pr_id := mux.Vars(r)["pullRequestId"]
if id, err := strconv.Atoi(pr_id); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
} else if id < 0 {
HandleError(w, http.StatusNotFound, map[string]interface{}{"errors": []interface{}{
map[string]interface{}{
"context": nil,
"message": "No pull request exists with ID -1 for this repository 1",
"exceptionName": "com.atlassian.bitbucket.pull.NoSuchPullRequestException",
},
}})
return
}
HandleRequest(w, r)
}
func HandleError(w http.ResponseWriter, httpStatus int, body interface{}) {
w.WriteHeader(httpStatus)
bodyData, err := json.Marshal(body)
if err == nil {
w.Write(bodyData)
}
}
func DeleteAvatar(w http.ResponseWriter, r *http.Request) {
HandleRequest(w, r)
}
@ -798,3 +874,7 @@ func SetCommitStatus(w http.ResponseWriter, r *http.Request) {
func SearchCode(w http.ResponseWriter, r *http.Request) {
HandleRequest(w, r)
}
func GetSSHKeys(w http.ResponseWriter, r *http.Request) {
HandleRequest(w, r)
}

View File

@ -1426,4 +1426,11 @@ var routes = Routes{
"/rest/search/latest/search",
SearchCode,
},
Route{
"GetSSHKeys",
strings.ToUpper("Get"),
"/rest/ssh/1.0/keys",
GetSSHKeys,
},
}

View File

@ -2,7 +2,6 @@ package swagger
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
@ -19,14 +18,9 @@ func HandleRequest(w http.ResponseWriter, r *http.Request) {
if routeName != "" {
handlerName = routeName
}
path, err := route.GetPathTemplate()
if err == nil {
fmt.Println(path)
}
}
fmt.Println(handlerName)
fileName := fmt.Sprintf("mocked_responses/%s.json", handlerName)
response, err := ioutil.ReadFile(fileName)
fileName := fmt.Sprintf("test/bb-mock-server/mocked_responses/%s.json", handlerName)
response, err := os.ReadFile(fileName)
if err == nil {
w.Write(response)
} else {

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,13 @@
{
"size": 1,
"limit": 25,
"isLastPage": true,
"values": [
{
"id": 1,
"text": "ssh-rsa AAAAB3... me@127.0.0.1",
"label": "me@127.0.0.1"
}
],
"start": 0
}

View File

@ -1 +1,16 @@
{}
{
"name": "admin",
"emailAddress": "admin@example.com",
"id": 1,
"displayName": "admin",
"active": true,
"slug": "admin",
"type": "NORMAL",
"links": {
"self": [
{
"href": "http://localhost:7990/users/admin"
}
]
}
}

View File

@ -0,0 +1,16 @@
{
"query": {
"substituted": false
},
"code": {
"category": "primary",
"count": 0,
"isLastPage": true,
"nextStart": 10,
"start": 0,
"values": []
},
"scope": {
"type": "GLOBAL"
}
}