Skip to content

Commit

Permalink
chore(integration): Create a package for each integration
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Nov 16, 2024
1 parent cdc7543 commit 38ad7ba
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
13 changes: 7 additions & 6 deletions internal/domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"gabe565.com/domain-watch/internal/config"
"gabe565.com/domain-watch/internal/integration"
"gabe565.com/domain-watch/internal/integration/telegram"
"github.com/go-telegram/bot"
whoisparser "github.com/likexian/whois-parser"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -75,14 +76,14 @@ func TestDomain_NotifyThreshold(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/bot123/sendMessage", r.URL.Path)
gotNotify = true
resp := integration.APIResponse{
resp := telegram.APIResponse{
OK: true,
Result: json.RawMessage("{}"),
}
assert.NoError(t, json.NewEncoder(w).Encode(&resp))
}))
t.Cleanup(server.Close)
telegram := integration.TelegramTestSetup(t, bot.WithServerURL(server.URL))
tg := telegram.NewTestClient(t, bot.WithServerURL(server.URL))

d := &Domain{
conf: &config.Config{Threshold: []int{1, 7}},
Expand All @@ -93,7 +94,7 @@ func TestDomain_NotifyThreshold(t *testing.T) {
TimeLeft: tt.fields.TimeLeft,
TriggeredThreshold: tt.fields.TriggeredThreshold,
}
tt.wantErr(t, d.NotifyThreshold(context.Background(), integration.Integrations{"telegram": telegram}))
tt.wantErr(t, d.NotifyThreshold(context.Background(), integration.Integrations{"telegram": tg}))
assert.Equal(t, tt.wantNotify, gotNotify)
})
}
Expand Down Expand Up @@ -148,14 +149,14 @@ func TestDomain_NotifyStatusChange(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/bot123/sendMessage", r.URL.Path)
gotNotify = true
resp := integration.APIResponse{
resp := telegram.APIResponse{
OK: true,
Result: json.RawMessage("{}"),
}
assert.NoError(t, json.NewEncoder(w).Encode(resp))
}))
t.Cleanup(server.Close)
telegram := integration.TelegramTestSetup(t, bot.WithServerURL(server.URL))
tg := telegram.NewTestClient(t, bot.WithServerURL(server.URL))

d := &Domain{
Name: tt.fields.Name,
Expand All @@ -164,7 +165,7 @@ func TestDomain_NotifyStatusChange(t *testing.T) {
TimeLeft: tt.fields.TimeLeft,
TriggeredThreshold: tt.fields.TriggeredThreshold,
}
tt.wantErr(t, d.NotifyStatusChange(context.Background(), integration.Integrations{"telegram": telegram}))
tt.wantErr(t, d.NotifyStatusChange(context.Background(), integration.Integrations{"telegram": tg}))
assert.Equal(t, tt.wantNotify, gotNotify)
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package gotify

import (
"bytes"
Expand Down
6 changes: 4 additions & 2 deletions internal/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log/slog"

"gabe565.com/domain-watch/internal/config"
"gabe565.com/domain-watch/internal/integration/gotify"
"gabe565.com/domain-watch/internal/integration/telegram"
"gabe565.com/domain-watch/internal/util"
)

Expand All @@ -18,8 +20,8 @@ type Integrations map[string]Integration

func Default() Integrations {
return map[string]Integration{
"telegram": &Telegram{},
"gotify": &Gotify{},
"telegram": &telegram.Telegram{},
"gotify": &gotify.Gotify{},
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package telegram

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration
package telegram

import (
"bytes"
Expand All @@ -24,7 +24,7 @@ type APIResponse struct {
} `json:"parameters,omitempty"`
}

func TelegramTestSetup(t *testing.T, opts ...bot.Option) *Telegram {
func NewTestClient(t *testing.T, opts ...bot.Option) *Telegram {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/bot123/getMe" {
var buf bytes.Buffer
Expand Down

0 comments on commit 38ad7ba

Please sign in to comment.