test(be): add view to dredd hooks and add view to swagger

This commit is contained in:
Denis Gukov 2021-10-27 17:40:52 +05:00
parent 76a81cc29c
commit 3445d9a716
4 changed files with 110 additions and 30 deletions

View File

@ -17,6 +17,7 @@ var userProject *db.Project
var userKey *db.AccessKey
var task *db.Task
var schedule *db.Schedule
var view *db.View
// Runtime created simple ID values for some items we need to reference in other objects
var repoID int64
@ -30,9 +31,10 @@ var capabilities = map[string][]string{
"repository": {"access_key"},
"inventory": {"repository"},
"environment": {"repository"},
"template": {"repository", "inventory", "environment"},
"task": {"template"},
"schedule": {"template"},
"template": {"repository", "inventory", "environment", "view"},
"task": {"template"},
"schedule": {"template"},
"view": {"repository"},
}
func capabilityWrapper(cap string) func(t *trans.Transaction) {
@ -66,6 +68,8 @@ func resolveCapability(caps []string, resolved []string, uid string) {
switch v {
case "schedule":
schedule = addSchedule()
case "view":
view = addView()
case "user":
userPathTestUser = addUser()
case "project":
@ -95,8 +99,8 @@ func resolveCapability(caps []string, resolved []string, uid string) {
environmentID, _ = res.LastInsertId()
case "template":
res, err := store.Sql().Exec(
"insert into project__template " +
"(project_id, inventory_id, repository_id, environment_id, alias, playbook, arguments, override_args, description) " +
"insert into project__template "+
"(project_id, inventory_id, repository_id, environment_id, alias, playbook, arguments, override_args, description) "+
"values (?, ?, ?, ?, ?, ?, ?, ?, ?)",
userProject.ID, inventoryID, repoID, environmentID, "Test-"+uid, "test-playbook.yml", "", false, "Hello, World!")
printError(err)
@ -126,6 +130,7 @@ var pathSubPatterns = []func() string{
func() string { return strconv.Itoa(int(templateID)) },
func() string { return strconv.Itoa(task.ID) },
func() string { return strconv.Itoa(schedule.ID) },
func() string { return strconv.Itoa(view.ID) },
}
// alterRequestPath with the above slice of functions
@ -164,7 +169,9 @@ func alterRequestBody(t *trans.Transaction) {
if schedule != nil {
bodyFieldProcessor("schedule_id", schedule.ID, &request)
}
if view != nil {
bodyFieldProcessor("view_id", view.ID, &request)
}
// Inject object ID to body for PUT requests
if strings.ToLower(t.Request.Method) == "put" {
putRequestPathRE := regexp.MustCompile(`/api/(?:project/\d+/)?\w+/(\d+)/?$`)

View File

@ -138,6 +138,21 @@ func addUser() *db.User {
return &user
}
func addView() *db.View {
view, err := store.CreateView(db.View{
ProjectID: userProject.ID,
Title: "Test",
Position: 1,
})
if err != nil {
fmt.Println(err)
}
return &view
}
func addSchedule() *db.Schedule {
schedule, err := store.CreateSchedule(db.Schedule{
TemplateID: int(templateID),

View File

@ -109,6 +109,10 @@ func main() {
h.Before("schedule > /api/project/{project_id}/schedules/{schedule_id} > Updates schedule > 204 > application/json", capabilityWrapper("schedule"))
h.Before("schedule > /api/project/{project_id}/schedules/{schedule_id} > Deletes schedule > 204 > application/json", capabilityWrapper("schedule"))
h.Before("view > /api/project/{project_id}/views/{view_id} > Get view > 200 > application/json", capabilityWrapper("view"))
h.Before("view > /api/project/{project_id}/views/{view_id} > Updates view > 204 > application/json", capabilityWrapper("view"))
h.Before("view > /api/project/{project_id}/views/{view_id} > Deletes view > 204 > application/json", capabilityWrapper("view"))
//Add these last as they normalize the requests and path values after hook processing
h.BeforeAll(func(transactions []*trans.Transaction) {
for _, t := range transactions {

View File

@ -302,6 +302,9 @@ definitions:
environment_id:
type: integer
minimum: 1
view_id:
type: integer
minimum: 1
alias:
type: string
playbook:
@ -330,6 +333,9 @@ definitions:
environment_id:
type: integer
minimum: 1
view_id:
type: integer
minimum: 1
alias:
type: string
playbook:
@ -369,44 +375,28 @@ definitions:
type: integer
InventoryRequest:
ViewRequest:
type: object
properties:
name:
title:
type: string
project_id:
type: integer
minimum: 1
inventory:
type: string
ssh_key_id:
position:
type: integer
minimum: 1
become_key_id:
type: integer
minimum: 1
type:
type: string
enum: [static, file]
Inventory:
View:
type: object
properties:
id:
type: integer
name:
title:
type: string
project_id:
type: integer
inventory:
type: string
ssh_key_id:
position:
type: integer
become_key_id:
type: integer
type:
type: string
enum: [static, file]
Event:
@ -563,8 +553,7 @@ paths:
tags:
- authentication
summary: Performs Login
description: |
Upon success you will be logged in
description: Upon success you will be logged in
security: [] # No security
parameters:
- name: Login Body
@ -1313,6 +1302,71 @@ paths:
schema:
$ref: "#/definitions/Schedule"
# project views
/project/{project_id}/views:
parameters:
- $ref: "#/parameters/project_id"
get:
tags:
- project
summary: Get view
responses:
200:
description: view
schema:
type: array
items:
$ref: "#/definitions/View"
post:
tags:
- project
summary: create view
parameters:
- name: view
in: body
required: true
schema:
$ref: "#/definitions/ViewRequest"
responses:
201:
description: view created
schema:
$ref: "#/definitions/View"
/project/{project_id}/views/{view_id}:
parameters:
- $ref: "#/parameters/project_id"
- $ref: "#/parameters/view_id"
get:
tags:
- project
summary: Get view
responses:
200:
description: view object
schema:
$ref: "#/definitions/View"
put:
tags:
- project
summary: Updates view
parameters:
- name: view
in: body
required: true
schema:
$ref: "#/definitions/ViewRequest"
responses:
204:
description: view updated
delete:
tags:
- project
summary: Removes view
responses:
204:
description: view removed
# tasks
/project/{project_id}/tasks: