| 1 | package matrix_test |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/require" |
| 8 | |
| 9 | "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix" |
| 10 | ) |
| 11 | |
| 12 | // TestServiceInitializeWithTokenOnly tests the token-only authentication flow. |
| 13 | // This tests the URL format matrix://:[email protected] where |
| 14 | // the username is empty and the password is the access token. |
| 15 | // We test via Config directly to avoid network calls. |
| 16 | func TestServiceInitializeWithTokenOnly(t *testing.T) { |
| 17 | t.Parallel() |
| 18 | |
| 19 | tests := []struct { |
| 20 | name string |
| 21 | serviceURL string |
| 22 | wantUser string |
| 23 | wantPass string |
| 24 | }{ |
| 25 | { |
| 26 | name: "token-only auth with access token", |
| 27 | serviceURL: "matrix://:[email protected]", |
| 28 | wantUser: "", |
| 29 | wantPass: "syt_xxx_token", |
| 30 | }, |
| 31 | { |
| 32 | name: "token-only auth with longer token", |
| 33 | serviceURL: "matrix://:[email protected]", |
| 34 | wantUser: "", |
| 35 | wantPass: "syt_1234567890abcdefghijklmnopqrstuvwxyz", |
| 36 | }, |
| 37 | { |
| 38 | name: "token-only auth with special chars in token", |
| 39 | serviceURL: "matrix://:[email protected]", |
| 40 | wantUser: "", |
| 41 | wantPass: "syt_test-token_123", |
| 42 | }, |
| 43 | } |
| 44 | |
| 45 | for _, tt := range tests { |
| 46 | t.Run(tt.name, func(t *testing.T) { |
| 47 | t.Parallel() |
| 48 | |
| 49 | parsedURL, err := url.Parse(tt.serviceURL) |
| 50 | require.NoError(t, err, "URL should be parseable for %s", tt.name) |
| 51 | |
| 52 | // Use Config directly to avoid network calls |
| 53 | config := &matrix.Config{} |
| 54 | err = config.SetURL(parsedURL) |
| 55 | |
| 56 | require.NoError(t, err, "Expected no error for %s", tt.name) |
| 57 | require.Equal(t, tt.wantUser, config.User, |
| 58 | "User should be empty for token-only auth in %s", tt.name) |
| 59 | require.Equal(t, tt.wantPass, config.Password, |
| 60 | "Password should be set to token for %s", tt.name) |
| 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // TestServiceInitializeWithUserAndToken tests that when both user and token |
| 66 | // are provided via config (not via service), the config is set correctly. |
| 67 | func TestServiceInitializeWithUserAndToken(t *testing.T) { |
| 68 | t.Parallel() |
| 69 | |
| 70 | tests := []struct { |
| 71 | name string |
| 72 | serviceURL string |
| 73 | wantUser string |
| 74 | wantPass string |
| 75 | }{ |
| 76 | { |
| 77 | name: "user and token provided", |
| 78 | serviceURL: "matrix://user:[email protected]", |
| 79 | wantUser: "user", |
| 80 | wantPass: "syt_xxx", |
| 81 | }, |
| 82 | { |
| 83 | name: "user with token as password", |
| 84 | serviceURL: "matrix://myuser:[email protected]", |
| 85 | wantUser: "myuser", |
| 86 | wantPass: "$ecret_t0k3n", |
| 87 | }, |
| 88 | } |
| 89 | |
| 90 | for _, tt := range tests { |
| 91 | t.Run(tt.name, func(t *testing.T) { |
| 92 | t.Parallel() |
| 93 | |
| 94 | parsedURL, err := url.Parse(tt.serviceURL) |
| 95 | require.NoError(t, err, "URL should be parseable for %s", tt.name) |
| 96 | |
| 97 | // Use Config directly to avoid network calls |
| 98 | config := &matrix.Config{} |
| 99 | err = config.SetURL(parsedURL) |
| 100 | |
| 101 | require.NoError(t, err, "Expected no error for %s", tt.name) |
| 102 | require.Equal(t, tt.wantUser, config.User, |
| 103 | "User mismatch for %s", tt.name) |
| 104 | require.Equal(t, tt.wantPass, config.Password, |
| 105 | "Password mismatch for %s", tt.name) |
| 106 | }) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // TestServiceInitializeTokenOnlyWithRooms tests token-only auth with room configuration. |
| 111 | // Rooms are set directly on the config struct, not via URL params (since URL only supports one room). |
| 112 | func TestServiceInitializeTokenOnlyWithRooms(t *testing.T) { |
| 113 | t.Parallel() |
| 114 | |
| 115 | tests := []struct { |
| 116 | name string |
| 117 | serviceURL string |
| 118 | rooms []string |
| 119 | expectedRooms []string |
| 120 | }{ |
| 121 | { |
| 122 | name: "token-only with room set directly", |
| 123 | serviceURL: "matrix://:[email protected]", |
| 124 | rooms: []string{"#test:example.com"}, |
| 125 | expectedRooms: []string{"#test:example.com"}, |
| 126 | }, |
| 127 | { |
| 128 | name: "token-only with multiple rooms set directly", |
| 129 | serviceURL: "matrix://:[email protected]", |
| 130 | rooms: []string{"#room1:example.com", "#room2:example.com"}, |
| 131 | expectedRooms: []string{"#room1:example.com", "#room2:example.com"}, |
| 132 | }, |
| 133 | } |
| 134 | |
| 135 | for _, tt := range tests { |
| 136 | t.Run(tt.name, func(t *testing.T) { |
| 137 | t.Parallel() |
| 138 | |
| 139 | parsedURL, err := url.Parse(tt.serviceURL) |
| 140 | require.NoError(t, err, "URL should be parseable for %s", tt.name) |
| 141 | |
| 142 | // Set rooms directly on config |
| 143 | config := &matrix.Config{ |
| 144 | Rooms: tt.rooms, |
| 145 | } |
| 146 | err = config.SetURL(parsedURL) |
| 147 | |
| 148 | require.NoError(t, err, "Expected no error for %s", tt.name) |
| 149 | require.Empty(t, config.User, |
| 150 | "User should be empty for token-only auth in %s", tt.name) |
| 151 | require.Equal(t, tt.expectedRooms, config.Rooms, |
| 152 | "Rooms mismatch for %s", tt.name) |
| 153 | }) |
| 154 | } |
| 155 | } |