summaryrefslogtreecommitdiff
path: root/tests/server/test_url_mapper.py
blob: a04e7593d83d2cee2578845ce876d734bfc74d9d (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
# test_url_mapper.py
# Copyright (C) 2017 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Tests for server-related functionality.
"""
import pytest

from twisted.trial import unittest
from uuid import uuid4

from leap.soledad.server.url_mapper import URLMapper


class URLMapperTestCase(unittest.TestCase):
    """
    Test if the URLMapper behaves as expected.

    The following table lists the authorized actions among all possible
    u1db remote actions:

        URL path                      | Authorized actions
        --------------------------------------------------
        /                             | GET
        /shared-db                    | GET
        /shared-db/docs               | -
        /shared-db/doc/{id}           | -
        /shared-db/sync-from/{source} | -
        /user-db                      | -
        /user-db/docs                 | -
        /user-db/doc/{id}             | -
        /user-db/sync-from/{source}   | GET, PUT, POST
    """

    def setUp(self):
        self._uuid = uuid4().hex
        self._urlmap = URLMapper()
        self._dbname = 'user-%s' % self._uuid

    @pytest.mark.needs_couch
    def test_root_authorized(self):
        match = self._urlmap.match('/', 'GET')
        self.assertIsNotNone(match)

    def test_shared_authorized(self):
        self.assertIsNotNone(self._urlmap.match('/shared', 'GET'))

    def test_shared_unauthorized(self):
        self.assertIsNone(self._urlmap.match('/shared', 'PUT'))
        self.assertIsNone(self._urlmap.match('/shared', 'DELETE'))
        self.assertIsNone(self._urlmap.match('/shared', 'POST'))

    def test_shared_docs_unauthorized(self):
        self.assertIsNone(self._urlmap.match('/shared/docs', 'GET'))
        self.assertIsNone(self._urlmap.match('/shared/docs', 'PUT'))
        self.assertIsNone(self._urlmap.match('/shared/docs', 'DELETE'))
        self.assertIsNone(self._urlmap.match('/shared/docs', 'POST'))

    def test_shared_doc_authorized(self):
        match = self._urlmap.match('/shared/doc/x', 'GET')
        self.assertIsNotNone(match)
        self.assertEqual('x', match.get('id'))

        match = self._urlmap.match('/shared/doc/x', 'PUT')
        self.assertIsNotNone(match)
        self.assertEqual('x', match.get('id'))

        match = self._urlmap.match('/shared/doc/x', 'DELETE')
        self.assertIsNotNone(match)
        self.assertEqual('x', match.get('id'))

    def test_shared_doc_unauthorized(self):
        self.assertIsNone(self._urlmap.match('/shared/doc/x', 'POST'))

    def test_shared_sync_unauthorized(self):
        self.assertIsNone(self._urlmap.match('/shared/sync-from/x', 'GET'))
        self.assertIsNone(self._urlmap.match('/shared/sync-from/x', 'PUT'))
        self.assertIsNone(self._urlmap.match('/shared/sync-from/x', 'DELETE'))
        self.assertIsNone(self._urlmap.match('/shared/sync-from/x', 'POST'))

    def test_user_db_unauthorized(self):
        dbname = self._dbname
        self.assertIsNone(self._urlmap.match('/%s' % dbname, 'GET'))
        self.assertIsNone(self._urlmap.match('/%s' % dbname, 'PUT'))
        self.assertIsNone(self._urlmap.match('/%s' % dbname, 'DELETE'))
        self.assertIsNone(self._urlmap.match('/%s' % dbname, 'POST'))

    def test_user_db_docs_unauthorized(self):
        dbname = self._dbname
        self.assertIsNone(self._urlmap.match('/%s/docs' % dbname, 'GET'))
        self.assertIsNone(self._urlmap.match('/%s/docs' % dbname, 'PUT'))
        self.assertIsNone(self._urlmap.match('/%s/docs' % dbname, 'DELETE'))
        self.assertIsNone(self._urlmap.match('/%s/docs' % dbname, 'POST'))

    def test_user_db_doc_unauthorized(self):
        dbname = self._dbname
        self.assertIsNone(self._urlmap.match('/%s/doc/x' % dbname, 'GET'))
        self.assertIsNone(self._urlmap.match('/%s/doc/x' % dbname, 'PUT'))
        self.assertIsNone(self._urlmap.match('/%s/doc/x' % dbname, 'DELETE'))
        self.assertIsNone(self._urlmap.match('/%s/doc/x' % dbname, 'POST'))

    def test_user_db_sync_authorized(self):
        uuid = self._uuid
        dbname = self._dbname
        match = self._urlmap.match('/%s/sync-from/x' % dbname, 'GET')
        self.assertEqual(uuid, match.get('uuid'))
        self.assertEqual('x', match.get('source_replica_uid'))

        match = self._urlmap.match('/%s/sync-from/x' % dbname, 'PUT')
        self.assertEqual(uuid, match.get('uuid'))
        self.assertEqual('x', match.get('source_replica_uid'))

        match = self._urlmap.match('/%s/sync-from/x' % dbname, 'POST')
        self.assertEqual(uuid, match.get('uuid'))
        self.assertEqual('x', match.get('source_replica_uid'))

    def test_user_db_sync_unauthorized(self):
        dbname = self._dbname
        self.assertIsNone(
            self._urlmap.match('/%s/sync-from/x' % dbname, 'DELETE'))