Browse Source

Merge pull request #203 from matrix-org/dbkr/test_all_the_things

Add support for matrix-is-tester
David Baker 4 years ago
parent
commit
ad6e5beb8b

+ 2 - 0
.gitignore

@@ -3,3 +3,5 @@
 
 sydent.conf
 sydent.db
+/matrix_is_test/sydent.stderr
+_trial_temp

+ 16 - 0
README.rst

@@ -28,6 +28,22 @@ Where country code is the numeric country code, or 'default' to specify the orig
     originators.44 = short:12345
     originators.default = alpha:Matrix
 
+Testing
+=======
+
+Sydent uses matrix-is-tester (https://github.com/matrix-org/matrix-is-tester/) to provide black-box testing of its API.
+This can be run as follows:
+
+    pip install git+https://github.com/matrix-org/matrix-is-tester.git
+    trial matrix_is_tester
+
+The SYDENT_PYTHON enviroment variable can be set to launch sydent with a specific python binary:
+
+    SYDENT_PYTHON=/path/to/python trial matrix_is_tester
+
+The matrix_is_test directory contains sydent's launcher for matrix_is_tester: this needs to be on the
+python path.
+
 Requests
 ========
 

+ 0 - 0
matrix_is_test/__init__.py


+ 98 - 0
matrix_is_test/launcher.py

@@ -0,0 +1,98 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2019 The Matrix.org Foundation C.I.C.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import os
+import tempfile
+import shutil
+import time
+from subprocess import Popen
+
+CFG_TEMPLATE = """
+[http]
+clientapi.http.bind_address = localhost
+clientapi.http.port = {port}
+client_http_base = http://localhost:{port}
+verify_response_template = {testsubject_path}/res/verify_response_template
+federation.verifycerts = False
+
+[db]
+db.file = :memory:
+
+[general]
+server.name = test.local
+terms.path = {terms_path}
+
+[email]
+email.tlsmode = 0
+email.template = {testsubject_path}/res/verification_template.eml
+email.invite.subject = %(sender_display_name)s has invited you to chat
+email.smtphost = localhost
+email.from = Sydent Validation <noreply@localhost>
+email.smtpport = 9925
+email.subject = Your Validation Token
+email.invite_template = {testsubject_path}/res/invite_template.eml
+"""
+
+class MatrixIsTestLauncher(object):
+    def __init__(self, with_terms):
+        self.with_terms = with_terms
+
+    def launch(self):
+        sydent_path = os.path.abspath(os.path.join(
+            os.path.dirname(__file__), '..',
+        ))
+        testsubject_path = os.path.join(
+            sydent_path, 'matrix_is_test',
+        )
+        terms_path = os.path.join(testsubject_path, 'terms.yaml') if self.with_terms else ''
+        port = 8099 if self.with_terms else 8098
+
+        self.tmpdir = tempfile.mkdtemp(prefix='sydenttest')
+
+        with open(os.path.join(self.tmpdir, 'sydent.conf'), 'w') as cfgfp:
+            cfgfp.write(CFG_TEMPLATE.format(
+                testsubject_path=testsubject_path,
+                terms_path=terms_path,
+                port=port,
+            ))
+
+        newEnv = os.environ.copy()
+        newEnv.update({
+            'PYTHONPATH': sydent_path,
+        })
+
+        stderr_fp = open(os.path.join(testsubject_path, 'sydent.stderr'), 'w')
+
+        pybin = os.getenv('SYDENT_PYTHON', 'python')
+
+        self.process = Popen(
+            args=[pybin, '-m', 'sydent.sydent'],
+            cwd=self.tmpdir,
+            env=newEnv,
+            stderr=stderr_fp,
+        )
+        # XXX: wait for startup in a sensible way
+        time.sleep(2)
+
+        self._baseUrl = 'http://localhost:%d' % (port,)
+
+    def tearDown(self):
+        print("Stopping sydent...")
+        self.process.terminate()
+        shutil.rmtree(self.tmpdir)
+
+    def get_base_url(self):
+        return self._baseUrl

+ 8 - 0
matrix_is_test/res/invite_template.eml

@@ -0,0 +1,8 @@
+{
+	"token": "%(token)s",
+	"room_alias": "%(room_alias)s",
+	"room_avatar_url": "%(room_avatar_url)s",
+	"room_name": "%(room_name)s",
+	"sender_display_name": "%(sender_display_name)s",
+	"sender_avatar_url": "%(sender_avatar_url)s"
+}

+ 1 - 0
matrix_is_test/res/verification_template.eml

@@ -0,0 +1 @@
+<<<%(token)s>>>

+ 1 - 0
matrix_is_test/res/verify_response_template

@@ -0,0 +1 @@
+matrix_is_tester:email_submit_get_response

+ 20 - 0
matrix_is_test/terms.yaml

@@ -0,0 +1,20 @@
+master_version: "someversion"
+docs:
+  terms_of_service:
+    version: "5.0"
+    langs:
+      en:
+        name: "Terms of Service"
+        url: "https://example.org/somewhere/terms-2.0-en.html"
+      fr:
+        name: "Conditions d'utilisation"
+        url: "https://example.org/somewhere/terms-2.0-fr.html"
+  privacy_policy:
+    version: "1.2"
+    langs:
+      en:
+        name: "Privacy Policy"
+        url: "https://example.org/somewhere/privacy-1.2-en.html"
+      fr:
+        name: "Politique de confidentialité"
+        url: "https://example.org/somewhere/privacy-1.2-fr.html"