Skip to content

Commit

Permalink
feat(gitprovider): move and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasMrqes committed Dec 20, 2024
1 parent 2bf2f08 commit 84441b0
Show file tree
Hide file tree
Showing 7 changed files with 463 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
"encoding/json"
"fmt"
"io"
"os"

"net/http"
"os"
"testing"

"github.com/padok-team/burrito/internal/utils/gitprovider/github"
"github.com/padok-team/burrito/internal/utils/gitprovider/types"
"github.com/padok-team/burrito/internal/webhook/event"
"github.com/padok-team/burrito/internal/webhook/github"

webhook "github.com/go-playground/webhooks/github"
"github.com/stretchr/testify/assert"
)

func TestGithub_GetEvent_PushEvent(t *testing.T) {
func TestGithub_GetEventFromWebhookPayload_PushEvent(t *testing.T) {
payloadFile, err := os.Open("testdata/github-push-main-event.json")
if err != nil {
t.Fatalf("failed to open payload file: %v", err)
Expand All @@ -44,10 +44,12 @@ func TestGithub_GetEvent_PushEvent(t *testing.T) {
}

secret := "test-secret"
github := github.Github{
Secret: secret,
github := &github.Github{
Config: types.Config{
WebhookSecret: secret,
},
}
err = github.Init()
err = github.InitWebhookHandler()
assert.NoError(t, err)

req.Header.Set("X-GitHub-Event", "push")
Expand All @@ -57,9 +59,10 @@ func TestGithub_GetEvent_PushEvent(t *testing.T) {
assert.NoError(t, err)
expectedMac := hex.EncodeToString(mac.Sum(nil))
req.Header.Set("X-Hub-Signature", fmt.Sprintf("sha1=%s", expectedMac))
parsed, ok := github.ParseFromProvider(req)

parsed, ok := github.ParseWebhookPayload(req)
assert.True(t, ok)
evt, err := github.GetEvent(parsed)
evt, err := github.GetEventFromWebhookPayload(parsed)
assert.NoError(t, err)
assert.IsType(t, &event.PushEvent{}, evt)

Expand All @@ -71,7 +74,7 @@ func TestGithub_GetEvent_PushEvent(t *testing.T) {
assert.ElementsMatch(t, []string{"modules/random-pets/main.tf", "terragrunt/random-pets/test/inputs.hcl", "modules/random-pets/variables.tf"}, pushEvt.Changes)
}

func TestGithub_GetEvent_PullRequestEvent(t *testing.T) {
func TestGithub_GetEventFromWebhookPayload_PullRequestEvent(t *testing.T) {
payloadFile, err := os.Open("testdata/github-open-pull-request-event.json")
if err != nil {
t.Fatalf("failed to open payload file: %v", err)
Expand All @@ -95,10 +98,12 @@ func TestGithub_GetEvent_PullRequestEvent(t *testing.T) {
}

secret := "test-secret"
github := github.Github{
Secret: secret,
github := &github.Github{
Config: types.Config{
WebhookSecret: secret,
},
}
err = github.Init()
err = github.InitWebhookHandler()
assert.NoError(t, err)

req.Header.Set("X-GitHub-Event", "pull_request")
Expand All @@ -109,9 +114,9 @@ func TestGithub_GetEvent_PullRequestEvent(t *testing.T) {
expectedMac := hex.EncodeToString(mac.Sum(nil))
req.Header.Set("X-Hub-Signature", fmt.Sprintf("sha1=%s", expectedMac))

parsed, ok := github.ParseFromProvider(req)
parsed, ok := github.ParseWebhookPayload(req)
assert.True(t, ok)
evt, err := github.GetEvent(parsed)
evt, err := github.GetEventFromWebhookPayload(parsed)
assert.NoError(t, err)
assert.IsType(t, &event.PullRequestEvent{}, evt)

Expand All @@ -123,3 +128,66 @@ func TestGithub_GetEvent_PullRequestEvent(t *testing.T) {
assert.Equal(t, "faf5e25402a9bd10f7318c8a2cd984af576c687f", pullRequestEvt.Commit)
assert.Equal(t, "opened", pullRequestEvt.Action)
}

func TestGithub_IsAvailable(t *testing.T) {
tests := []struct {
name string
config types.Config
capabilities []string
want bool
}{
{
name: "GitHub App credentials",
config: types.Config{
AppID: 123,
AppInstallationID: 456,
AppPrivateKey: "test-key",
URL: "https://github.com/org/repo",
},
capabilities: []string{types.Capabilities.Clone, types.Capabilities.Comment},
want: true,
},
{
name: "GitHub Token",
config: types.Config{
GitHubToken: "test-token",
URL: "https://github.com/org/repo",
},
capabilities: []string{types.Capabilities.Clone, types.Capabilities.Comment},
want: true,
},
{
name: "Webhook only with secret",
config: types.Config{
WebhookSecret: "secret",
URL: "https://github.com/org/repo",
},
capabilities: []string{types.Capabilities.Webhook},
want: true,
},
{
name: "Unsupported capability",
config: types.Config{
GitHubToken: "test-token",
URL: "https://github.com/org/repo",
},
capabilities: []string{"unsupported"},
want: false,
},
{
name: "No authentication",
config: types.Config{
URL: "https://github.com/org/repo",
},
capabilities: []string{types.Capabilities.Clone},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := github.IsAvailable(tt.config, tt.capabilities)
assert.Equal(t, tt.want, got)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import (
"bytes"
"encoding/json"
"io"
"os"

"net/http"
"os"
"testing"

"github.com/padok-team/burrito/internal/utils/gitprovider/gitlab"
"github.com/padok-team/burrito/internal/utils/gitprovider/types"
"github.com/padok-team/burrito/internal/webhook/event"
"github.com/padok-team/burrito/internal/webhook/gitlab"

webhook "github.com/go-playground/webhooks/gitlab"
"github.com/stretchr/testify/assert"
)

func TestGitlab_GetEvent_PushEvent(t *testing.T) {
func TestGitlab_GetEventFromWebhookPayload_PushEvent(t *testing.T) {
payloadFile, err := os.Open("testdata/gitlab-push-main-event.json")
if err != nil {
t.Fatalf("failed to open payload file: %v", err)
Expand All @@ -40,18 +40,20 @@ func TestGitlab_GetEvent_PushEvent(t *testing.T) {
}

secret := "test-secret"
gitlab := gitlab.Gitlab{
Secret: secret,
gitlab := &gitlab.Gitlab{
Config: types.Config{
WebhookSecret: secret,
},
}
err = gitlab.Init()
err = gitlab.InitWebhookHandler()
assert.NoError(t, err)

req.Header.Set("X-GitLab-Event", "Push Hook")
req.Header.Set("X-Gitlab-Token", secret)

parsed, ok := gitlab.ParseFromProvider(req)
parsed, ok := gitlab.ParseWebhookPayload(req)
assert.True(t, ok)
evt, err := gitlab.GetEvent(parsed)
evt, err := gitlab.GetEventFromWebhookPayload(parsed)
assert.NoError(t, err)
assert.IsType(t, &event.PushEvent{}, evt)

Expand All @@ -63,16 +65,7 @@ func TestGitlab_GetEvent_PushEvent(t *testing.T) {
assert.ElementsMatch(t, []string{"test.hcl", "layer-1/prod.hcl", "layer-2/staging.hcl"}, pushEvt.Changes)
}

func TestGitlab_GetEvent_MergeRequestEvent(t *testing.T) {
// Test GitLab initialization
secret := "test-secret"
gitlab := gitlab.Gitlab{
Secret: secret,
}
err := gitlab.Init()
assert.NoError(t, err)

// Test event handling
func TestGitlab_GetEventFromWebhookPayload_MergeRequestEvent(t *testing.T) {
payloadFile, err := os.Open("testdata/gitlab-open-merge-request-event.json")
if err != nil {
t.Fatalf("failed to open payload file: %v", err)
Expand Down Expand Up @@ -100,12 +93,21 @@ func TestGitlab_GetEvent_MergeRequestEvent(t *testing.T) {
t.Fatalf("failed to create request: %v", err)
}

secret := "test-secret"
gitlab := &gitlab.Gitlab{
Config: types.Config{
WebhookSecret: secret,
},
}
err = gitlab.InitWebhookHandler()
assert.NoError(t, err)

req.Header.Set("X-GitLab-Event", "Merge Request Hook")
req.Header.Set("X-Gitlab-Token", secret)

parsed, ok := gitlab.ParseFromProvider(req)
parsed, ok := gitlab.ParseWebhookPayload(req)
assert.True(t, ok)
evt, err := gitlab.GetEvent(parsed)
evt, err := gitlab.GetEventFromWebhookPayload(parsed)
assert.NoError(t, err)
assert.IsType(t, &event.PullRequestEvent{}, evt)

Expand All @@ -123,3 +125,65 @@ func TestGitlab_GetEvent_MergeRequestEvent(t *testing.T) {
testWithGivenAction("close", event.PullRequestClosed)
testWithGivenAction("merge", event.PullRequestClosed)
}

func TestGitlab_IsAvailable(t *testing.T) {
tests := []struct {
name string
config types.Config
capabilities []string
want bool
}{
{
name: "GitLab Token",
config: types.Config{
GitLabToken: "test-token",
URL: "https://gitlab.com/org/repo",
},
capabilities: []string{types.Capabilities.Clone, types.Capabilities.Comment},
want: true,
},
{
name: "Webhook only with secret",
config: types.Config{
WebhookSecret: "secret",
URL: "https://gitlab.com/org/repo",
},
capabilities: []string{types.Capabilities.Webhook},
want: true,
},
{
name: "Unsupported capability",
config: types.Config{
GitLabToken: "test-token",
URL: "https://gitlab.com/org/repo",
},
capabilities: []string{"unsupported"},
want: false,
},
{
name: "No authentication",
config: types.Config{
URL: "https://gitlab.com/org/repo",
},
capabilities: []string{types.Capabilities.Clone},
want: false,
},
{
name: "Basic auth not supported",
config: types.Config{
Username: "user",
Password: "pass",
URL: "https://gitlab.com/org/repo",
},
capabilities: []string{types.Capabilities.Clone},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := gitlab.IsAvailable(tt.config, tt.capabilities)
assert.Equal(t, tt.want, got)
})
}
}
Loading

0 comments on commit 84441b0

Please sign in to comment.