| 1 |
package e2e_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"net/url" |
| 6 |
"os" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/onsi/ginkgo/v2" |
| 12 |
"github.com/onsi/gomega" |
| 13 |
) |
| 14 |
|
| 15 |
// Test_Zulip_E2E runs the Zulip E2E test suite using Ginkgo. |
| 16 |
// These tests connect to a real Zulip server and verify actual behavior. |
| 17 |
// |
| 18 |
// Environment variables: |
| 19 |
// - SHOUTRRR_ZULIP_HOST: Zulip server hostname (default: localhost) |
| 20 |
// - SHOUTRRR_ZULIP_BOT_MAIL: Bot email address |
| 21 |
// - SHOUTRRR_ZULIP_BOT_KEY: Bot API key |
| 22 |
// - SHOUTRRR_ZULIP_STREAM: Target stream name (optional) |
| 23 |
// - SHOUTRRR_ZULIP_TOPIC: Message topic (optional) |
| 24 |
// - SHOUTRRR_ZULIP_DM_TO: Comma-separated recipients (emails or user IDs) for direct message tests (optional; falls back to bot mail for self-DM to exercise the feature) |
| 25 |
// - SHOUTRRR_ZULIP_TYPE: Message type (channel/direct) for tests (optional) |
| 26 |
// - SHOUTRRR_ZULIP_READ_BY_SENDER: "true" to set read_by_sender (optional) |
| 27 |
func Test_Zulip_E2E(t *testing.T) { //nolint:paralleltest // mutates process-wide env via loadEnvFile |
| 28 |
loadEnvFile(".env") |
| 29 |
|
| 30 |
gomega.RegisterFailHandler(ginkgo.Fail) |
| 31 |
|
| 32 |
ginkgo.BeforeEach(func() { |
| 33 |
time.Sleep(250 * time.Millisecond) |
| 34 |
}) |
| 35 |
|
| 36 |
ginkgo.RunSpecs(t, "Zulip E2E Tests") |
| 37 |
} |
| 38 |
|
| 39 |
// buildServiceURL constructs the Zulip service URL from environment variables. |
| 40 |
func buildServiceURL() string { |
| 41 |
host := os.Getenv("SHOUTRRR_ZULIP_HOST") |
| 42 |
if host == "" { |
| 43 |
host = "localhost" |
| 44 |
} |
| 45 |
|
| 46 |
botMail := os.Getenv("SHOUTRRR_ZULIP_BOT_MAIL") |
| 47 |
botKey := os.Getenv("SHOUTRRR_ZULIP_BOT_KEY") |
| 48 |
|
| 49 |
if botMail == "" || botKey == "" { |
| 50 |
return "" |
| 51 |
} |
| 52 |
|
| 53 |
serviceURL := &url.URL{ |
| 54 |
Scheme: "zulip", |
| 55 |
User: url.UserPassword(botMail, botKey), |
| 56 |
Host: host, |
| 57 |
} |
| 58 |
|
| 59 |
q := url.Values{} |
| 60 |
if stream := os.Getenv("SHOUTRRR_ZULIP_STREAM"); stream != "" { |
| 61 |
q.Set("stream", stream) |
| 62 |
} |
| 63 |
|
| 64 |
if topic := os.Getenv("SHOUTRRR_ZULIP_TOPIC"); topic != "" { |
| 65 |
q.Set("topic", topic) |
| 66 |
} |
| 67 |
|
| 68 |
if typ := os.Getenv("SHOUTRRR_ZULIP_TYPE"); typ != "" { |
| 69 |
q.Set("type", typ) |
| 70 |
} |
| 71 |
|
| 72 |
if to := os.Getenv("SHOUTRRR_ZULIP_DM_TO"); to != "" { |
| 73 |
q.Set("to", to) |
| 74 |
} |
| 75 |
|
| 76 |
if read := os.Getenv("SHOUTRRR_ZULIP_READ_BY_SENDER"); read == "true" { |
| 77 |
q.Set("read_by_sender", "true") |
| 78 |
} |
| 79 |
|
| 80 |
if len(q) > 0 { |
| 81 |
serviceURL.RawQuery = q.Encode() |
| 82 |
} |
| 83 |
|
| 84 |
return serviceURL.String() |
| 85 |
} |
| 86 |
|
| 87 |
// loadEnvFile loads environment variables from a .env file. |
| 88 |
func loadEnvFile(filename string) { |
| 89 |
file, err := os.Open(filename) |
| 90 |
if err != nil { |
| 91 |
return |
| 92 |
} |
| 93 |
|
| 94 |
defer func() { _ = file.Close() }() |
| 95 |
|
| 96 |
scanner := bufio.NewScanner(file) |
| 97 |
for scanner.Scan() { |
| 98 |
line := strings.TrimSpace(scanner.Text()) |
| 99 |
if line == "" || strings.HasPrefix(line, "#") { |
| 100 |
continue |
| 101 |
} |
| 102 |
|
| 103 |
parts := strings.SplitN(line, "=", 2) |
| 104 |
if len(parts) == 2 { |
| 105 |
key := strings.TrimSpace(parts[0]) |
| 106 |
value := strings.TrimSpace(parts[1]) |
| 107 |
|
| 108 |
value = strings.Trim(value, `"'`) |
| 109 |
if _, exists := os.LookupEnv(key); !exists { |
| 110 |
_ = os.Setenv(key, value) |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
_ = scanner.Err() |
| 116 |
} |