feat: notification provider gotify
This commit is contained in:
81
pkg/notificationProvider/gotify/gotify_test.go
Normal file
81
pkg/notificationProvider/gotify/gotify_test.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package notificationProviderGotify_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
gotify "realdnydns/pkg/notificationProvider/gotify"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
t.Run("URL is required", testNewEmptyUrl())
|
||||
t.Run("Token is required", testNewEmptyToken())
|
||||
t.Run("Priority must be between 0 and 4", testNewInvalidPriority())
|
||||
t.Run("Sends POST request to url", testNewSendsPostRequest())
|
||||
}
|
||||
|
||||
func testNewEmptyUrl() func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
_, err := gotify.New(gotify.NotificationProviderImplGotifyConfig{
|
||||
Token: "1234",
|
||||
Priority: 1,
|
||||
})
|
||||
|
||||
if err.Error() != "url is required" {
|
||||
t.Errorf("Expected error 'url is required', got %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testNewEmptyToken() func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
_, err := gotify.New(gotify.NotificationProviderImplGotifyConfig{
|
||||
Url: "http://localhost:1234",
|
||||
Priority: 0,
|
||||
})
|
||||
|
||||
if err.Error() != "token is required" {
|
||||
t.Errorf("Expected error 'token is required', got %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testNewInvalidPriority() func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
_, err := gotify.New(gotify.NotificationProviderImplGotifyConfig{
|
||||
Url: "http://localhost:1234",
|
||||
Token: "token",
|
||||
Priority: 5,
|
||||
})
|
||||
|
||||
if err.Error() != "priority must be between 0 and 4" {
|
||||
t.Errorf("Expected error 'priority must be between 0 and 4', got %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testNewSendsPostRequest() func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
mockServer := httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(200)
|
||||
},
|
||||
))
|
||||
defer mockServer.Close()
|
||||
|
||||
provider, err := gotify.New(gotify.NotificationProviderImplGotifyConfig{
|
||||
Url: mockServer.URL,
|
||||
Token: "1234",
|
||||
Priority: 0,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("New() returned unexpected error: %v", err)
|
||||
}
|
||||
|
||||
err = provider.SendNotification("title", "message")
|
||||
if err != nil {
|
||||
t.Fatalf("SendNotification() returned unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user