| 1 |
package matrix |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/url" |
| 5 |
|
| 6 |
"github.com/onsi/gomega" |
| 7 |
|
| 8 |
ginkgo "github.com/onsi/ginkgo/v2" |
| 9 |
) |
| 10 |
|
| 11 |
var _ = ginkgo.Describe("Config", func() { |
| 12 |
ginkgo.Describe("GetURL", func() { |
| 13 |
ginkgo.It("should generate URL with scheme and host", func() { |
| 14 |
cfg := &Config{ |
| 15 |
Host: "matrix.example.com", |
| 16 |
User: "testuser", |
| 17 |
Password: "testpass", |
| 18 |
} |
| 19 |
|
| 20 |
resultURL := cfg.GetURL() |
| 21 |
gomega.Expect(resultURL.Scheme).To(gomega.Equal(Scheme)) |
| 22 |
gomega.Expect(resultURL.Host).To(gomega.Equal("matrix.example.com")) |
| 23 |
}) |
| 24 |
|
| 25 |
ginkgo.It("should include user in URL", func() { |
| 26 |
cfg := &Config{ |
| 27 |
Host: "matrix.example.com", |
| 28 |
User: "testuser", |
| 29 |
Password: "testpass", |
| 30 |
} |
| 31 |
|
| 32 |
resultURL := cfg.GetURL() |
| 33 |
gomega.Expect(resultURL.User.Username()).To(gomega.Equal("testuser")) |
| 34 |
}) |
| 35 |
|
| 36 |
ginkgo.It("should include password in URL", func() { |
| 37 |
cfg := &Config{ |
| 38 |
Host: "matrix.example.com", |
| 39 |
User: "testuser", |
| 40 |
Password: "testpass", |
| 41 |
} |
| 42 |
|
| 43 |
resultURL := cfg.GetURL() |
| 44 |
password, ok := resultURL.User.Password() |
| 45 |
gomega.Expect(ok).To(gomega.BeTrue()) |
| 46 |
gomega.Expect(password).To(gomega.Equal("testpass")) |
| 47 |
}) |
| 48 |
|
| 49 |
ginkgo.It("should set ForceQuery to true", func() { |
| 50 |
cfg := &Config{ |
| 51 |
Host: "matrix.example.com", |
| 52 |
User: "testuser", |
| 53 |
Password: "testpass", |
| 54 |
} |
| 55 |
|
| 56 |
resultURL := cfg.GetURL() |
| 57 |
gomega.Expect(resultURL.ForceQuery).To(gomega.BeTrue()) |
| 58 |
}) |
| 59 |
}) |
| 60 |
|
| 61 |
ginkgo.Describe("SetURL", func() { |
| 62 |
ginkgo.It("should parse valid URL and set fields", func() { |
| 63 |
cfg := &Config{} |
| 64 |
testURL, err := url.Parse("matrix://testuser:[email protected]?disableTLS=true") |
| 65 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 66 |
|
| 67 |
err = cfg.SetURL(testURL) |
| 68 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 69 |
gomega.Expect(cfg.Host).To(gomega.Equal("matrix.example.com")) |
| 70 |
gomega.Expect(cfg.User).To(gomega.Equal("testuser")) |
| 71 |
gomega.Expect(cfg.Password).To(gomega.Equal("testpass")) |
| 72 |
}) |
| 73 |
|
| 74 |
ginkgo.It("should return error when host is missing", func() { |
| 75 |
cfg := &Config{} |
| 76 |
testURL, err := url.Parse("matrix://testuser:testpass@") |
| 77 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 78 |
|
| 79 |
err = cfg.SetURL(testURL) |
| 80 |
gomega.Expect(err).To(gomega.Equal(ErrMissingHost)) |
| 81 |
}) |
| 82 |
|
| 83 |
ginkgo.It("should return error when password is missing", func() { |
| 84 |
cfg := &Config{} |
| 85 |
testURL, err := url.Parse("matrix://[email protected]") |
| 86 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 87 |
|
| 88 |
err = cfg.SetURL(testURL) |
| 89 |
gomega.Expect(err).To(gomega.Equal(ErrMissingCredentials)) |
| 90 |
}) |
| 91 |
|
| 92 |
ginkgo.It("should prepend # to room aliases that don't have prefix", func() { |
| 93 |
cfg := &Config{} |
| 94 |
testURL, err := url.Parse("matrix://testuser:[email protected]?room=testroom") |
| 95 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 96 |
|
| 97 |
err = cfg.SetURL(testURL) |
| 98 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 99 |
gomega.Expect(cfg.Rooms).ToNot(gomega.BeEmpty()) |
| 100 |
gomega.Expect(cfg.Rooms[0]).To(gomega.Equal("#testroom")) |
| 101 |
}) |
| 102 |
|
| 103 |
ginkgo.It("should not prepend # to room IDs that already have ! prefix", func() { |
| 104 |
cfg := &Config{} |
| 105 |
testURL, err := url.Parse("matrix://testuser:[email protected]?room=!testroom:matrix.example.com") |
| 106 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 107 |
|
| 108 |
err = cfg.SetURL(testURL) |
| 109 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 110 |
gomega.Expect(cfg.Rooms[0]).To(gomega.Equal("!testroom:matrix.example.com")) |
| 111 |
}) |
| 112 |
|
| 113 |
ginkgo.It("should not double-prefix room aliases that already have # prefix", func() { |
| 114 |
cfg := &Config{} |
| 115 |
testURL, err := url.Parse("matrix://testuser:[email protected]?room=%23myroom:matrix.example.com") |
| 116 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 117 |
|
| 118 |
err = cfg.SetURL(testURL) |
| 119 |
gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 120 |
gomega.Expect(cfg.Rooms).ToNot(gomega.BeEmpty()) |
| 121 |
gomega.Expect(cfg.Rooms[0]).To(gomega.Equal("#myroom:matrix.example.com")) |
| 122 |
}) |
| 123 |
}) |
| 124 |
}) |