Skip to content

Commit

Permalink
fix(test): enable webhook integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Dec 2, 2023
1 parent efed81e commit d7c8f81
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
48 changes: 38 additions & 10 deletions testscript/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testscript
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -45,7 +46,7 @@ func TestMain(m *testing.M) {
binPath = filepath.Join(tmp, "soft")

// Build the soft binary with -cover flag.
cmd := exec.Command("go", "build", "-o", binPath, "-cover", filepath.Join("..", "cmd", "soft"))
cmd := exec.Command("go", "build", "-cover", "-o", binPath, filepath.Join("..", "cmd", "soft"))
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "failed to build soft-serve binary: %s", err)
os.Exit(1)
Expand Down Expand Up @@ -75,14 +76,15 @@ func TestScript(t *testing.T) {
Dir: "./testdata/",
UpdateScripts: *update,
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
"soft": cmdSoft(admin1.Signer()),
"usoft": cmdSoft(user1.Signer()),
"git": cmdGit(key),
"curl": cmdCurl,
"mkfile": cmdMkfile,
"envfile": cmdEnvfile,
"readfile": cmdReadfile,
"dos2unix": cmdDos2Unix,
"soft": cmdSoft(admin1.Signer()),
"usoft": cmdSoft(user1.Signer()),
"git": cmdGit(key),
"curl": cmdCurl,
"mkfile": cmdMkfile,
"envfile": cmdEnvfile,
"readfile": cmdReadfile,
"dos2unix": cmdDos2Unix,
"new-webhook": cmdNewWebhook,
},
Setup: func(e *testscript.Env) error {
data := t.TempDir()
Expand Down Expand Up @@ -156,7 +158,10 @@ func TestScript(t *testing.T) {

ctx := config.WithContext(context.Background(), cfg)

cmd := exec.CommandContext(ctx, binPath, "serve")
// XXX: Right now, --sync-hooks is the only flag option we have for
// the serve command.
// TODO: Find a way to test different flag options.
cmd := exec.CommandContext(ctx, binPath, "serve", "--sync-hooks")
cmd.Dir = e.WorkDir
cmd.Env = e.Vars
cmd.Stderr = os.Stderr
Expand Down Expand Up @@ -318,6 +323,29 @@ func cmdEnvfile(ts *testscript.TestScript, neg bool, args []string) {
}
}

func cmdNewWebhook(ts *testscript.TestScript, neg bool, args []string) {
type webhookSite struct {
UUID string `json:"uuid"`
}

if len(args) != 1 {
ts.Fatalf("usage: new-webhook <env-name>")
}

const whSite = "https://webhook.site"
req, err := http.NewRequest(http.MethodPost, whSite+"/token", nil)
check(ts, err, neg)

resp, err := http.DefaultClient.Do(req)
check(ts, err, neg)

defer resp.Body.Close()
var site webhookSite
check(ts, json.NewDecoder(resp.Body).Decode(&site), neg)

ts.Setenv(args[0], whSite+"/"+site.UUID)
}

func cmdCurl(ts *testscript.TestScript, neg bool, args []string) {
var verbose bool
var headers []string
Expand Down
14 changes: 6 additions & 8 deletions testscript/testdata/repo-webhooks.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ stderr 'Created repository repo-123.*'
stdout ssh://localhost:$SSH_PORT/repo-123.git

# create webhook
soft repo webhook create repo-123 https://webhook.site/794fa12b-08d4-4362-a0a9-a6f995f22e17 -e branch_tag_create -e branch_tag_delete -e collaborator -e push -e repository -e repository_visibility_change
new-webhook WH_REPO_123
soft repo webhook create repo-123 $WH_REPO_123 -e branch_tag_create -e branch_tag_delete -e collaborator -e push -e repository -e repository_visibility_change

# list webhooks
soft repo webhook list repo-123
stdout '1.*https://webhook.site/794fa12b-08d4-4362-a0a9-a6f995f22e17.*'
stdout '1.*webhook.site/.*'

# clone repo
# clone repo and commit files
git clone ssh://localhost:$SSH_PORT/repo-123 repo-123

# create files
mkfile ./repo-123/README.md 'foobar'
git -C repo-123 add -A
git -C repo-123 commit -m 'first'
git -C repo-123 push origin HEAD

# list webhook deliveries
# TODO: enable this test when githooks tests are fixed
# soft repo webhook deliver list repo-123 1
# stdout '.*https://webhook.site/.*'
soft repo webhook deliver list repo-123 1
stdout '✅.*push.*'

0 comments on commit d7c8f81

Please sign in to comment.