Skip to content

Commit

Permalink
fix: fix test with missing access token (#75)
Browse files Browse the repository at this point in the history
* fix: fix test with missing access token

* test: Repository without Deployment Configuration

* test: Repository without Deployment Configuration
  • Loading branch information
kimdre authored Aug 6, 2024
1 parent d0c0d30 commit 7615d65
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions cmd/doco-cd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/docker/compose/v2/pkg/api"
Expand All @@ -24,6 +25,7 @@ func TestHandleEvent(t *testing.T) {
payload webhook.ParsedPayload
expectedStatusCode int
expectedResponseBody string
overrideEnv map[string]string
}{
{
name: "Successful Deployment",
Expand All @@ -37,6 +39,7 @@ func TestHandleEvent(t *testing.T) {
},
expectedStatusCode: http.StatusCreated,
expectedResponseBody: `{"details":"project deployment successful","job_id":"%s"}%s`,
overrideEnv: nil,
},
{
name: "Invalid Reference",
Expand All @@ -50,6 +53,7 @@ func TestHandleEvent(t *testing.T) {
},
expectedStatusCode: http.StatusInternalServerError,
expectedResponseBody: `{"error":"failed to clone repository","details":"couldn't find remote ref \"refs/heads/invalid\"","job_id":"%s"}%s`,
overrideEnv: nil,
},
{
name: "Private Repository",
Expand All @@ -63,11 +67,51 @@ func TestHandleEvent(t *testing.T) {
},
expectedStatusCode: http.StatusCreated,
expectedResponseBody: `{"details":"project deployment successful","job_id":"%s"}%s`,
overrideEnv: nil,
},
{
name: "Private Repository with missing Git Access Token",
payload: webhook.ParsedPayload{
Ref: "refs/heads/main",
CommitSHA: "26263c2b44133367927cd1423d8c8457b5befce5",
Name: "doco-cd",
FullName: "kimdre/doco-cd",
CloneURL: "https://github.com/kimdre/doco-cd",
Private: true,
},
expectedStatusCode: http.StatusInternalServerError,
expectedResponseBody: `{"error":"missing access token for private repository","job_id":"%s"}%s`,
overrideEnv: map[string]string{
"GIT_ACCESS_TOKEN": "",
},
},
{
name: "Missing Deployment Configuration",
payload: webhook.ParsedPayload{
Ref: "refs/heads/main",
CommitSHA: "efefb4111f3c363692a2526f9be9b24560e6511f",
Name: "doco-cd",
FullName: "kimdre/kimdre",
CloneURL: "https://github.com/kimdre/kimdre",
Private: false,
},
expectedStatusCode: http.StatusInternalServerError,
expectedResponseBody: `{"error":"no compose files found","details":"stat /tmp/kimdre/kimdre/docker-compose.yaml: no such file or directory","job_id":"%s"}%s`,
overrideEnv: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.overrideEnv != nil {
for k, v := range tc.overrideEnv {
err := os.Setenv(k, v)
if err != nil {
t.Fatalf("Failed to set environment variable: %v", err)
}
}
}

appConfig, _ := config.GetAppConfig()

log := logger.New(12)
Expand Down

0 comments on commit 7615d65

Please sign in to comment.