| 1 |
// Copyright (c) 2021 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package id_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
|
| 15 |
"maunium.net/go/mautrix/id" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
roomIDLink = id.MatrixURI{Sigil1: '!', MXID1: "7NdBVvkd4aLSbgKt9RXl:example.org"} |
| 20 |
roomIDViaLink = id.MatrixURI{Sigil1: '!', MXID1: "7NdBVvkd4aLSbgKt9RXl:example.org", Via: []string{"maunium.net", "matrix.org"}} |
| 21 |
roomAliasLink = id.MatrixURI{Sigil1: '#', MXID1: "someroom:example.org"} |
| 22 |
roomIDEventLink = id.MatrixURI{Sigil1: '!', MXID1: "7NdBVvkd4aLSbgKt9RXl:example.org", Sigil2: '$', MXID2: "uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s"} |
| 23 |
roomAliasEventLink = id.MatrixURI{Sigil1: '#', MXID1: "someroom:example.org", Sigil2: '$', MXID2: "uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s"} |
| 24 |
userLink = id.MatrixURI{Sigil1: '@', MXID1: "user:example.org"} |
| 25 |
) |
| 26 |
|
| 27 |
func TestMatrixURI_MatrixToURL(t *testing.T) { |
| 28 |
assert.Equal(t, "https://matrix.to/#/%217NdBVvkd4aLSbgKt9RXl%3Aexample.org", roomIDLink.MatrixToURL()) |
| 29 |
assert.Equal(t, "https://matrix.to/#/%217NdBVvkd4aLSbgKt9RXl%3Aexample.org?via=maunium.net&via=matrix.org", roomIDViaLink.MatrixToURL()) |
| 30 |
assert.Equal(t, "https://matrix.to/#/%23someroom%3Aexample.org", roomAliasLink.MatrixToURL()) |
| 31 |
assert.Equal(t, "https://matrix.to/#/%217NdBVvkd4aLSbgKt9RXl%3Aexample.org/%24uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s", roomIDEventLink.MatrixToURL()) |
| 32 |
assert.Equal(t, "https://matrix.to/#/%23someroom%3Aexample.org/%24uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s", roomAliasEventLink.MatrixToURL()) |
| 33 |
assert.Equal(t, "https://matrix.to/#/%40user%3Aexample.org", userLink.MatrixToURL()) |
| 34 |
} |
| 35 |
|
| 36 |
func TestMatrixURI_String(t *testing.T) { |
| 37 |
assert.Equal(t, "matrix:roomid/7NdBVvkd4aLSbgKt9RXl:example.org", roomIDLink.String()) |
| 38 |
assert.Equal(t, "matrix:roomid/7NdBVvkd4aLSbgKt9RXl:example.org?via=maunium.net&via=matrix.org", roomIDViaLink.String()) |
| 39 |
assert.Equal(t, "matrix:r/someroom:example.org", roomAliasLink.String()) |
| 40 |
assert.Equal(t, "matrix:roomid/7NdBVvkd4aLSbgKt9RXl:example.org/e/uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s", roomIDEventLink.String()) |
| 41 |
assert.Equal(t, "matrix:r/someroom:example.org/e/uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s", roomAliasEventLink.String()) |
| 42 |
assert.Equal(t, "matrix:u/user:example.org", userLink.String()) |
| 43 |
} |
| 44 |
|
| 45 |
func TestParseMatrixURIOrMatrixToURL(t *testing.T) { |
| 46 |
const inputURI = "matrix:u/user:example.org" |
| 47 |
const inputMatrixToURL = "https://matrix.to/#/%40user%3Aexample.org" |
| 48 |
parsed1, err := id.ParseMatrixURIOrMatrixToURL(inputURI) |
| 49 |
require.NoError(t, err) |
| 50 |
require.NotNil(t, parsed1) |
| 51 |
parsed2, err := id.ParseMatrixURIOrMatrixToURL(inputMatrixToURL) |
| 52 |
require.NoError(t, err) |
| 53 |
require.NotNil(t, parsed2) |
| 54 |
|
| 55 |
assert.Equal(t, parsed1, parsed2) |
| 56 |
assert.Equal(t, inputURI, parsed2.String()) |
| 57 |
assert.Equal(t, inputMatrixToURL, parsed1.MatrixToURL()) |
| 58 |
} |
| 59 |
|
| 60 |
func TestParseMatrixURI_RoomAlias(t *testing.T) { |
| 61 |
parsed1, err := id.ParseMatrixURI("matrix:r/someroom:example.org") |
| 62 |
require.NoError(t, err) |
| 63 |
require.NotNil(t, parsed1) |
| 64 |
parsed2, err := id.ParseMatrixURI("matrix:room/someroom:example.org") |
| 65 |
require.NoError(t, err) |
| 66 |
require.NotNil(t, parsed2) |
| 67 |
|
| 68 |
assert.Equal(t, roomAliasLink, *parsed1) |
| 69 |
assert.Equal(t, roomAliasLink, *parsed2) |
| 70 |
} |
| 71 |
|
| 72 |
func TestParseMatrixURI_RoomID(t *testing.T) { |
| 73 |
parsed, err := id.ParseMatrixURI("matrix:roomid/7NdBVvkd4aLSbgKt9RXl:example.org") |
| 74 |
require.NoError(t, err) |
| 75 |
require.NotNil(t, parsed) |
| 76 |
parsedVia, err := id.ParseMatrixURI("matrix:roomid/7NdBVvkd4aLSbgKt9RXl:example.org?via=maunium.net&via=matrix.org") |
| 77 |
require.NoError(t, err) |
| 78 |
require.NotNil(t, parsedVia) |
| 79 |
|
| 80 |
assert.Equal(t, roomIDLink, *parsed) |
| 81 |
assert.Equal(t, roomIDViaLink, *parsedVia) |
| 82 |
} |
| 83 |
|
| 84 |
func TestParseMatrixURI_UserID(t *testing.T) { |
| 85 |
parsed1, err := id.ParseMatrixURI("matrix:u/user:example.org") |
| 86 |
require.NoError(t, err) |
| 87 |
require.NotNil(t, parsed1) |
| 88 |
parsed2, err := id.ParseMatrixURI("matrix:user/user:example.org") |
| 89 |
require.NoError(t, err) |
| 90 |
require.NotNil(t, parsed2) |
| 91 |
|
| 92 |
assert.Equal(t, userLink, *parsed1) |
| 93 |
assert.Equal(t, userLink, *parsed2) |
| 94 |
} |
| 95 |
|
| 96 |
func TestParseMatrixURI_EventID(t *testing.T) { |
| 97 |
parsed1, err := id.ParseMatrixURI("matrix:r/someroom:example.org/e/uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s") |
| 98 |
require.NoError(t, err) |
| 99 |
require.NotNil(t, parsed1) |
| 100 |
parsed2, err := id.ParseMatrixURI("matrix:room/someroom:example.org/e/uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s") |
| 101 |
require.NoError(t, err) |
| 102 |
require.NotNil(t, parsed2) |
| 103 |
parsed3, err := id.ParseMatrixURI("matrix:roomid/7NdBVvkd4aLSbgKt9RXl:example.org/e/uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s") |
| 104 |
require.NoError(t, err) |
| 105 |
require.NotNil(t, parsed3) |
| 106 |
|
| 107 |
assert.Equal(t, roomAliasEventLink, *parsed1) |
| 108 |
assert.Equal(t, roomAliasEventLink, *parsed2) |
| 109 |
assert.Equal(t, roomIDEventLink, *parsed3) |
| 110 |
} |
| 111 |
|
| 112 |
func TestParseMatrixToURL_RoomAlias(t *testing.T) { |
| 113 |
parsed, err := id.ParseMatrixToURL("https://matrix.to/#/#someroom:example.org") |
| 114 |
require.NoError(t, err) |
| 115 |
require.NotNil(t, parsed) |
| 116 |
parsedEncoded, err := id.ParseMatrixToURL("https://matrix.to/#/%23someroom%3Aexample.org") |
| 117 |
require.NoError(t, err) |
| 118 |
require.NotNil(t, parsedEncoded) |
| 119 |
|
| 120 |
assert.Equal(t, roomAliasLink, *parsed) |
| 121 |
assert.Equal(t, roomAliasLink, *parsedEncoded) |
| 122 |
} |
| 123 |
|
| 124 |
func TestParseMatrixToURL_RoomID(t *testing.T) { |
| 125 |
parsed, err := id.ParseMatrixToURL("https://matrix.to/#/!7NdBVvkd4aLSbgKt9RXl:example.org") |
| 126 |
require.NoError(t, err) |
| 127 |
require.NotNil(t, parsed) |
| 128 |
parsedEncoded, err := id.ParseMatrixToURL("https://matrix.to/#/%217NdBVvkd4aLSbgKt9RXl%3Aexample.org") |
| 129 |
require.NoError(t, err) |
| 130 |
require.NotNil(t, parsedEncoded) |
| 131 |
parsedVia, err := id.ParseMatrixToURL("https://matrix.to/#/!7NdBVvkd4aLSbgKt9RXl:example.org?via=maunium.net&via=matrix.org") |
| 132 |
require.NoError(t, err) |
| 133 |
require.NotNil(t, parsedVia) |
| 134 |
parsedViaEncoded, err := id.ParseMatrixToURL("https://matrix.to/#/%217NdBVvkd4aLSbgKt9RXl%3Aexample.org?via=maunium.net&via=matrix.org") |
| 135 |
require.NoError(t, err) |
| 136 |
require.NotNil(t, parsedViaEncoded) |
| 137 |
|
| 138 |
assert.Equal(t, roomIDLink, *parsed) |
| 139 |
assert.Equal(t, roomIDLink, *parsedEncoded) |
| 140 |
assert.Equal(t, roomIDViaLink, *parsedVia) |
| 141 |
assert.Equal(t, roomIDViaLink, *parsedViaEncoded) |
| 142 |
} |
| 143 |
|
| 144 |
func TestParseMatrixToURL_UserID(t *testing.T) { |
| 145 |
parsed, err := id.ParseMatrixToURL("https://matrix.to/#/@user:example.org") |
| 146 |
require.NoError(t, err) |
| 147 |
require.NotNil(t, parsed) |
| 148 |
parsedEncoded, err := id.ParseMatrixToURL("https://matrix.to/#/%40user%3Aexample.org") |
| 149 |
require.NoError(t, err) |
| 150 |
require.NotNil(t, parsedEncoded) |
| 151 |
|
| 152 |
assert.Equal(t, userLink, *parsed) |
| 153 |
assert.Equal(t, userLink, *parsedEncoded) |
| 154 |
} |
| 155 |
|
| 156 |
func TestParseMatrixToURL_EventID(t *testing.T) { |
| 157 |
parsed1, err := id.ParseMatrixToURL("https://matrix.to/#/#someroom:example.org/$uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s") |
| 158 |
require.NoError(t, err) |
| 159 |
require.NotNil(t, parsed1) |
| 160 |
parsed2, err := id.ParseMatrixToURL("https://matrix.to/#/!7NdBVvkd4aLSbgKt9RXl:example.org/$uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s") |
| 161 |
require.NoError(t, err) |
| 162 |
require.NotNil(t, parsed2) |
| 163 |
parsed1Encoded, err := id.ParseMatrixToURL("https://matrix.to/#/%23someroom:example.org/%24uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s") |
| 164 |
require.NoError(t, err) |
| 165 |
require.NotNil(t, parsed1) |
| 166 |
parsed2Encoded, err := id.ParseMatrixToURL("https://matrix.to/#/%217NdBVvkd4aLSbgKt9RXl:example.org/%24uOH4C9cK4HhMeFWkUXMbdF_dtndJ0j9je-kIK3XpV1s") |
| 167 |
require.NoError(t, err) |
| 168 |
require.NotNil(t, parsed2) |
| 169 |
|
| 170 |
assert.Equal(t, roomAliasEventLink, *parsed1) |
| 171 |
assert.Equal(t, roomAliasEventLink, *parsed1Encoded) |
| 172 |
assert.Equal(t, roomIDEventLink, *parsed2) |
| 173 |
assert.Equal(t, roomIDEventLink, *parsed2Encoded) |
| 174 |
} |