summary refs log tree commit diff
path: root/tests/storage/test_appservice.py
blob: 2ad55c8462a2ac0d5dd864ea8d1a1b7856447804 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-
# Copyright 2015 OpenMarket Ltd
#
# 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.
from tests import unittest
from twisted.internet import defer

from synapse.appservice import ApplicationService
from synapse.storage.appservice import ApplicationServiceStore

from tests.utils import setup_test_homeserver


class ApplicationServiceStoreTestCase(unittest.TestCase):

    @defer.inlineCallbacks
    def setUp(self):
        hs = yield setup_test_homeserver()
        db_pool = hs.get_db_pool()

        self.as_token = "token1"
        db_pool.runQuery(
            "INSERT INTO application_services(token) VALUES(?)",
            (self.as_token,)
        )
        db_pool.runQuery(
            "INSERT INTO application_services(token) VALUES(?)", ("token2",)
        )
        db_pool.runQuery(
            "INSERT INTO application_services(token) VALUES(?)", ("token3",)
        )
        # must be done after inserts
        self.store = ApplicationServiceStore(hs)

    @defer.inlineCallbacks
    def test_update_and_retrieval_of_service(self):
        url = "https://matrix.org/appservices/foobar"
        hs_token = "hstok"
        user_regex = [
            {"regex": "@foobar_.*:matrix.org", "exclusive": True}
        ]
        alias_regex = [
            {"regex": "#foobar_.*:matrix.org", "exclusive": False}
        ]
        room_regex = [

        ]
        service = ApplicationService(
            url=url, hs_token=hs_token, token=self.as_token, namespaces={
                ApplicationService.NS_USERS: user_regex,
                ApplicationService.NS_ALIASES: alias_regex,
                ApplicationService.NS_ROOMS: room_regex
        })
        yield self.store.update_app_service(service)

        stored_service = yield self.store.get_app_service_by_token(
            self.as_token
        )
        self.assertEquals(stored_service.token, self.as_token)
        self.assertEquals(stored_service.url, url)
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ALIASES],
            alias_regex
        )
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ROOMS],
            room_regex
        )
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_USERS],
            user_regex
        )

    @defer.inlineCallbacks
    def test_retrieve_unknown_service_token(self):
        service = yield self.store.get_app_service_by_token("invalid_token")
        self.assertEquals(service, None)

    @defer.inlineCallbacks
    def test_retrieval_of_service(self):
        stored_service = yield self.store.get_app_service_by_token(
            self.as_token
        )
        self.assertEquals(stored_service.token, self.as_token)
        self.assertEquals(stored_service.url, None)
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ALIASES],
            []
        )
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_ROOMS],
            []
        )
        self.assertEquals(
            stored_service.namespaces[ApplicationService.NS_USERS],
            []
        )

    @defer.inlineCallbacks
    def test_retrieval_of_all_services(self):
        services = yield self.store.get_app_services()
        self.assertEquals(len(services), 3)