Skip to content

Commit

Permalink
Add tests for preview_url and config authenticated endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Nov 25, 2023
1 parent f65a5a0 commit 9e50e95
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 0 deletions.
91 changes: 91 additions & 0 deletions test/msc3916_misc_client_endpoints_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package test

import (
"log"
"net/http"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/turt2live/matrix-media-repo/test/test_internals"
)

type MSC3916MiscClientEndpointsSuite struct {
suite.Suite
deps *test_internals.ContainerDeps
htmlPage *test_internals.HostedFile
}

func (s *MSC3916MiscClientEndpointsSuite) SetupSuite() {
deps, err := test_internals.MakeTestDeps()
if err != nil {
log.Fatal(err)
}
s.deps = deps

file, err := test_internals.ServeFile("index.html", deps, "<h1>This is a test file</h1>")
if err != nil {
log.Fatal(err)
}
s.htmlPage = file
}

func (s *MSC3916MiscClientEndpointsSuite) TearDownSuite() {
if s.htmlPage != nil {
s.htmlPage.Teardown()
}
if s.deps != nil {
if s.T().Failed() {
s.deps.Debug()
}
s.deps.Teardown()
}
}

func (s *MSC3916MiscClientEndpointsSuite) TestPreviewUrlRequiresAuth() {
t := s.T()

client1 := s.deps.Homeservers[0].UnprivilegedUsers[0].WithCsUrl(s.deps.Machines[0].HttpUrl)
client2 := &test_internals.MatrixClient{
ClientServerUrl: s.deps.Machines[0].HttpUrl,
ServerName: s.deps.Homeservers[0].ServerName,
AccessToken: "", // no auth on this client
UserId: "", // no auth on this client
}

qs := url.Values{
"url": []string{s.htmlPage.PublicUrl},
}
raw, err := client2.DoRaw("GET", "/_matrix/client/unstable/org.matrix.msc3916/media/preview_url", qs, "", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusUnauthorized, raw.StatusCode)

raw, err = client1.DoRaw("GET", "/_matrix/client/unstable/org.matrix.msc3916/media/preview_url", qs, "", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, raw.StatusCode)
}

func (s *MSC3916MiscClientEndpointsSuite) TestConfigRequiresAuth() {
t := s.T()

client1 := s.deps.Homeservers[0].UnprivilegedUsers[0].WithCsUrl(s.deps.Machines[0].HttpUrl)
client2 := &test_internals.MatrixClient{
ClientServerUrl: s.deps.Machines[0].HttpUrl,
ServerName: s.deps.Homeservers[0].ServerName,
AccessToken: "", // no auth on this client
UserId: "", // no auth on this client
}

raw, err := client2.DoRaw("GET", "/_matrix/client/unstable/org.matrix.msc3916/media/config", nil, "", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusUnauthorized, raw.StatusCode)

raw, err = client1.DoRaw("GET", "/_matrix/client/unstable/org.matrix.msc3916/media/config", nil, "", nil)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, raw.StatusCode)
}

func TestMSC3916MiscClientEndpointsSuite(t *testing.T) {
suite.Run(t, new(MSC3916MiscClientEndpointsSuite))
}
17 changes: 17 additions & 0 deletions test/templates/mmr.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,20 @@ datastores:
ssl: false
rateLimit:
enabled: false # we've got tests which intentionally spam
urlPreviews:
enabled: true
maxPageSizeBytes: 10485760
previewUnsafeCertificates: false
numWords: 50
maxLength: 200
numTitleWords: 30
maxTitleLength: 150
filePreviewTypes:
- "image/*"
numWorkers: 10
disallowedNetworks: []
allowedNetworks: ["0.0.0.0/0"]
expireAfterDays: 0
defaultLanguage: "en-US,en"
userAgent: "matrix-media-repo"
oEmbed: true
82 changes: 82 additions & 0 deletions test/test_internals/host_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package test_internals

import (
"fmt"
"log"
"os"
"path"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

type HostedFile struct {
upstream *ContainerDeps
nginx testcontainers.Container
tempDirectoryPath string

PublicUrl string
}

func ServeFile(fileName string, deps *ContainerDeps, contents string) (*HostedFile, error) {
tmp, err := os.MkdirTemp(os.TempDir(), "mmr-nginx")
if err != nil {
return nil, err
}

f, err := os.Create(path.Join(tmp, fileName))
if err != nil {
return nil, err
}
defer func(f *os.File) {
_ = f.Close()
}(f)

_, err = f.Write([]byte(contents))
if err != nil {
return nil, err
}

err = f.Close()
if err != nil {
return nil, err
}

nginx, err := testcontainers.GenericContainer(deps.ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "docker.io/library/nginx:latest",
ExposedPorts: []string{"80/tcp"},
Mounts: []testcontainers.ContainerMount{
testcontainers.BindMount(tmp, "/usr/share/nginx/html"),
},
Networks: []string{deps.depNet.NetId},
WaitingFor: wait.ForListeningPort("80/tcp"),
},
Started: true,
})
if err != nil {
return nil, err
}

nginxIp, err := nginx.ContainerIP(deps.ctx)
if err != nil {
return nil, err
}

//goland:noinspection HttpUrlsUsage
return &HostedFile{
upstream: deps,
nginx: nginx,
tempDirectoryPath: tmp,
PublicUrl: fmt.Sprintf("http://%s:%d/%s", nginxIp, 80, fileName),
}, nil
}

func (f *HostedFile) Teardown() {
if err := f.nginx.Terminate(f.upstream.ctx); err != nil {
log.Fatalf("Error shutting down nginx container: %s", err.Error())
}
if err := os.RemoveAll(f.tempDirectoryPath); err != nil {
log.Fatalf("Error cleaning up temporarily hosted file: %s", err.Error())
}
}

0 comments on commit 9e50e95

Please sign in to comment.