Skip to content

Commit 245ef4e

Browse files
committed
[e2e] add tests
1 parent 3b00774 commit 245ef4e

File tree

8 files changed

+948
-3
lines changed

8 files changed

+948
-3
lines changed

internal/commands/webhooks/register.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package webhooks
22

33
import (
44
"fmt"
5+
"net/url"
56
"strings"
67

78
"github.com/android-sms-gateway/cli/internal/core/codes"
@@ -41,17 +42,23 @@ var register = &cli.Command{
4142
},
4243
},
4344
Action: func(c *cli.Context) error {
44-
url := c.Args().Get(0)
45-
if url == "" {
45+
targetUrl := strings.TrimSpace(c.Args().Get(0))
46+
if targetUrl == "" {
4647
return cli.Exit("URL is empty", codes.ParamsError)
4748
}
4849

50+
// accept only absolute http/https URLs
51+
parsed, err := url.Parse(targetUrl)
52+
if err != nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") {
53+
return cli.Exit("invalid URL", codes.ParamsError)
54+
}
55+
4956
client := metadata.GetClient(c.App.Metadata)
5057
renderer := metadata.GetRenderer(c.App.Metadata)
5158

5259
req := smsgateway.Webhook{
5360
ID: c.String("id"),
54-
URL: url,
61+
URL: targetUrl,
5562
Event: c.String("event"),
5663
}
5764

tests/e2e/general_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package e2e
2+
3+
import (
4+
"bytes"
5+
"os/exec"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestHelpFlag(t *testing.T) {
12+
// Run the CLI binary with the --help flag
13+
var stdout, stderr bytes.Buffer
14+
15+
cmd := exec.Command("./smsgate", "--help")
16+
cmd.Stdout = &stdout
17+
cmd.Stderr = &stderr
18+
19+
err := cmd.Run()
20+
21+
assert.NoError(t, err)
22+
23+
// Verify the output
24+
assert.Contains(t, stdout.String(), "CLI interface for working with SMS Gateway for Android™")
25+
}

tests/e2e/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module e2e
2+
3+
go 1.24.3
4+
5+
require github.com/stretchr/testify v1.10.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

tests/e2e/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
6+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

tests/e2e/main_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package e2e
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"testing"
7+
)
8+
9+
func TestMain(m *testing.M) {
10+
// Build the CLI binary
11+
cmd := exec.Command("go", "build", "-o", "tests/e2e/smsgate", "cmd/smsgate/smsgate.go")
12+
cmd.Dir = "../../"
13+
err := cmd.Run()
14+
if err != nil {
15+
panic(err)
16+
}
17+
18+
code := m.Run()
19+
20+
_ = os.Remove("smsgate")
21+
22+
os.Exit(code)
23+
}

0 commit comments

Comments
 (0)