Skip to content

Commit

Permalink
Update Dredd changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-marschke committed Jan 15, 2024
1 parent 0c895c9 commit 412c28f
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 190 deletions.
48 changes: 13 additions & 35 deletions .dredd/hooks/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -43,7 +44,7 @@ var capabilities = map[string][]string{
"template": {"repository", "inventory", "environment", "view"},
"task": {"template"},
"schedule": {"template"},
"webhook": {"project", "user", "template"},
"webhook": {"project", "template"},
"webhookextractor": {"webhook"},
"webhookextractvalue": {"webhookextractor"},
"webhookmatcher": {"webhookextractor"},
Expand Down Expand Up @@ -145,42 +146,16 @@ func resolveCapability(caps []string, resolved []string, uid string) {
case "task":
task = addTask()
case "webhook":
webhook, err := store.CreateWebhook(db.Webhook{
ProjectID: userProject.ID,
Name: "Test Webhook",
TemplateID: templateID,
})
printError(err)
webhook = addWebhook()
webhookID = webhook.ID
case "webhookextractor":
webhookextractor, err := store.CreateWebhookExtractor(db.WebhookExtractor{
WebhookID: webhookID,
Name: "WebhookExtractor",
})
printError(err)
webhookextractor = addWebhookExtractor()
webhookExtractorID = webhookextractor.ID
case "webhookextractvalue":
webhookextractvalue, err := store.CreateWebhookExtractValue(db.WebhookExtractValue{
Name: "Value",
ExtractorID: webhookExtractorID,
ValueSource: "body",
BodyDataType: "json",
Key: "key",
Variable: "var",
})
printError(err)
webhookextractvalue = addWebhookExtractValue()
webhookExtractValueID = webhookextractvalue.ID
case "webhookmatcher":
webhookmatch, err := store.CreateWebhookMatcher(db.WebhookMatcher{
Name: "matcher",
ExtractorID: webhookExtractorID,
MatchType: "body",
Method: "equals",
BodyDataType: "json",
Key: "key",
Value: "value",
})
printError(err)
webhookmatch = addWebhookMatcher()
webhookMatchID = webhookmatch.ID
default:
panic("unknown capability " + v)
Expand Down Expand Up @@ -220,8 +195,10 @@ func alterRequestPath(t *trans.Transaction) {
exploded := make([]string, len(pathArgs))
copy(exploded, pathArgs)
for k, v := range pathSubPatterns {

pos, exists := stringInSlice(strconv.Itoa(k+1), exploded)
if exists {
fmt.Println(fmt.Sprintf("Current: %v, %v", pos, exploded))
pathArgs[pos] = v()
}
}
Expand Down Expand Up @@ -253,17 +230,18 @@ func alterRequestBody(t *trans.Transaction) {
if view != nil {
bodyFieldProcessor("view_id", view.ID, &request)
}

if webhook != nil {
bodyFieldProcessor("webhook_id", webhookID, &request)
bodyFieldProcessor("webhook_id", webhook.ID, &request)
}
if webhookextractor != nil {
bodyFieldProcessor("extractor_id", webhookExtractorID, &request)
bodyFieldProcessor("extractor_id", webhookextractor.ID, &request)
}
if webhookextractvalue != nil {
bodyFieldProcessor("value_id", webhookExtractValueID, &request)
bodyFieldProcessor("value_id", webhookextractvalue.ID, &request)
}
if webhookmatch != nil {
bodyFieldProcessor("matcher_id", webhookMatchID, &request)
bodyFieldProcessor("matcher_id", webhookmatch.ID, &request)
}

// Inject object ID to body for PUT requests
Expand Down
61 changes: 61 additions & 0 deletions .dredd/hooks/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,67 @@ func addTask() *db.Task {
return &t
}

func addWebhook() *db.Webhook {
webhook, err := store.CreateWebhook(db.Webhook{
ProjectID: userProject.ID,
Name: "Test Webhook",
TemplateID: templateID,
})
if err != nil {
panic(err)
}

return &webhook
}

func addWebhookExtractor() *db.WebhookExtractor {
webhookextractor, err := store.CreateWebhookExtractor(db.WebhookExtractor{
WebhookID: int(webhookID),
Name: "Webhook Extractor",
})

if err != nil {
panic(err)
}

return &webhookextractor
}

func addWebhookExtractValue() *db.WebhookExtractValue {
webhookextractvalue, err := store.CreateWebhookExtractValue(db.WebhookExtractValue{
Name: "Value",
ExtractorID: webhookExtractorID,
ValueSource: db.WebhookExtractBodyValue,
BodyDataType: db.WebhookBodyDataJSON,
Key: "key",
Variable: "var",
})

if err != nil {
panic(err)
}

return &webhookextractvalue
}

func addWebhookMatcher() *db.WebhookMatcher {
webhookmatch, err := store.CreateWebhookMatcher(db.WebhookMatcher{
Name: "matcher",
ExtractorID: webhookExtractorID,
MatchType: "body",
Method: "equals",
BodyDataType: "json",
Key: "key",
Value: "value",
})

if err != nil {
panic(err)
}

return &webhookmatch
}

// Token Handling
func addToken(tok string, user int) {
_, err := store.CreateAPIToken(db.APIToken{
Expand Down
10 changes: 8 additions & 2 deletions .dredd/hooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func main() {
transaction.Request.Body = "{ \"user_id\": " + strconv.Itoa(userPathTestUser.ID) + ",\"role\": \"owner\"}"
})

h.Before("project > /api/project/{project_id}/webhooks > get all webhooks > 200 > application/json", func(t *trans.Transaction) {
addCapabilities([]string{"repository", "inventory", "environment", "template", "webhook"})
})

h.Before("project > /api/project/{project_id}/webhook/{webhook_id} > Get Webhook > 200 > application/json", func(t *trans.Transaction) {
addCapabilities([]string{"repository", "inventory", "environment", "template", "webhook"})
})

h.Before("project > /api/project/{project_id}/keys/{key_id} > Updates access key > 204 > application/json", capabilityWrapper("access_key"))
h.Before("project > /api/project/{project_id}/keys/{key_id} > Removes access key > 204 > application/json", capabilityWrapper("access_key"))

Expand Down Expand Up @@ -112,8 +120,6 @@ 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("project > /api/project/{project_id}/webhooks > Add Webhook > 204 > application/json", capabilityWrapper("webhook"))

h.Before("project > /api/project/{project_id}/views/{view_id} > Get view > 200 > application/json", capabilityWrapper("view"))
h.Before("project > /api/project/{project_id}/views/{view_id} > Updates view > 204 > application/json", capabilityWrapper("view"))
h.Before("project > /api/project/{project_id}/views/{view_id} > Removes view > 204 > application/json", capabilityWrapper("view"))
Expand Down
4 changes: 2 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ tasks:
desc: Runs Packr for static assets
sources:
- web/dist/*
- db/migrations/*
- db/sql/migrations/*
generates:
- db/db-packr.go
- db/sql/db-packr.go
- api/api-packr.go
cmds:
- mkdir -p web/dist
Expand Down
Loading

0 comments on commit 412c28f

Please sign in to comment.