diff --git a/.github/workflows/ci-test.yml b/.github/workflows/ci-test.yml index aafbe02..5559924 100644 --- a/.github/workflows/ci-test.yml +++ b/.github/workflows/ci-test.yml @@ -20,6 +20,12 @@ jobs: - name: Check out code uses: actions/checkout@v3 + - name: Download Go modules + run: go mod tidy + + - name: Build packages + run: go build ./... + - name: golangci-lint uses: golangci/golangci-lint-action@v8 with: diff --git a/go.mod b/go.mod index 9e71deb..1e54d42 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module github.com/MowlCoder/heimdall go 1.24.2 + +require github.com/stretchr/testify v1.11.1 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..e0362f4 --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,83 @@ +package config + +import ( + "testing" + + "github.com/MowlCoder/heimdall/internal/domain" + "github.com/stretchr/testify/assert" +) + +func Test_Enablement_Notifiers(t *testing.T) { + + cfg := &Config{ + + Services: []domain.Service{ + + {Name: "My Personal Website", URL: "https://example.com"}, + }, + Notifiers: struct { + Telegram *struct { + ChatID string "json:\"chatId\"" + BotToken string "json:\"botToken\"" + Enabled bool "json:\"enabled\"" + } + Discord *struct { + Webhook string "json:\"webhook\"" + Enabled bool "json:\"enabled\"" + } + Slack *struct { + ChatID string "json:\"chatId\"" + BotToken string "json:\"botToken\"" + Enabled bool "json:\"enabled\"" + } + Webhook *struct { + Webhook string "json:\"webhook\"" + Enabled bool "json:\"enabled\"" + } + }{ + + Telegram: &struct { + ChatID string "json:\"chatId\"" + BotToken string "json:\"botToken\"" + Enabled bool "json:\"enabled\"" + }{ + + ChatID: "-123456789", + BotToken: "secret-telegram-bot-token", + Enabled: true, + }, + Slack: &struct { + ChatID string "json:\"chatId\"" + BotToken string "json:\"botToken\"" + Enabled bool "json:\"enabled\"" + }{ + ChatID: "RandomTest", + BotToken: "xoxosecret-slack-bot-token", + Enabled: true, + }, + Discord: &struct { + Webhook string "json:\"webhook\"" + Enabled bool "json:\"enabled\"" + }{ + Webhook: "random", + Enabled: true, + }, + Webhook: &struct { + Webhook string "json:\"webhook\"" + Enabled bool "json:\"enabled\"" + }{ + Webhook: "randomWebhook", + Enabled: true, + }, + }, + } + + checkTelegram := cfg.IsTelegramEnabled() + assert.True(t, checkTelegram) + checkSlack := cfg.IsSlackEnabled() + assert.True(t, checkSlack) + checkDiscord := cfg.IsDiscordEnabled() + assert.True(t, checkDiscord) + checkWebhook := cfg.IsWebhookEnabled() + assert.True(t, checkWebhook) +} diff --git a/internal/domain/service_test.go b/internal/domain/service_test.go new file mode 100644 index 0000000..2e04456 --- /dev/null +++ b/internal/domain/service_test.go @@ -0,0 +1,74 @@ +package domain + +import ( + "testing" +) + +func Test_ParseInterval(t *testing.T) { + + TestCases := []Service{ + { + + Name: "TestIntervalString", + Interval: "5m", + }, + { + + Name: "TestNumber", + Interval: "1", + }, + { + + Name: "TestRandom", + Interval: "1m", + }, + } + + for _, tc := range TestCases { + + t.Run(tc.Name, func(t *testing.T) { + + _, err := tc.ParseInterval() + + if err != nil { + + t.Errorf("Test case failed: Failed to parse the Interval %v", err) + } + + }) + + } + +} + +func Test_ParseIntervalOnlyString(t *testing.T) { + + TestCases := []Service{ + { + + Name: "TestIntervalString", + Interval: "m", + }, + { + + Name: "TestNumber", + Interval: "s", + }, + } + + for _, tc := range TestCases { + + t.Run(tc.Name, func(t *testing.T) { + + _, err := tc.ParseInterval() + + if err == nil { + + t.Error("Only String Interval pasrse, Test Case Failed") + } + + }) + + } + +} diff --git a/internal/notifier/utils_test.go b/internal/notifier/utils_test.go new file mode 100644 index 0000000..4e7f2cd --- /dev/null +++ b/internal/notifier/utils_test.go @@ -0,0 +1,35 @@ +package notifier + +import ( + "fmt" + "testing" +) + +func Test_StatusCheck(t *testing.T) { + + TestCases := []int{200, 400, 300, 500} + statusCodeTextMap := map[int]string{ + 200: "Success", + 300: "Redirect", + 400: "Client Error", + 500: "Server Error", + } + + for _, tc := range TestCases { + + t.Run("Status Code Text test", func(t *testing.T) { + + text := getStatusText(tc) + + if text != statusCodeTextMap[tc] { + + fmt.Println(statusCodeTextMap[tc]) + + t.Errorf("Text to status code mapping is incorrect for %v", tc) + } + + }) + + } + +}