summaryrefslogtreecommitdiff
path: root/qvisqve/file_store_tests.py
blob: 6d27afc22ae419a95646a905f44aec37cfd35202 (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
# Copyright (C) 2018  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import shutil
import tempfile
import unittest

import qvisqve


class FileStoreTests(unittest.TestCase):

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        self.fs = qvisqve.FileStore(self.tempdir)

    def tearDown(self):
        shutil.rmtree(self.tempdir)

    def test_has_no_clients_initially(self):
        self.assertEqual(self.fs.list('client'), [])

    def test_has_no_users_initially(self):
        self.assertEqual(self.fs.list('user'), [])

    def test_raises_error_getting_nonexistent_resource(self):
        with self.assertRaises(qvisqve.ResourceDoesNotExist):
            self.fs.get('client', 'does-not-exist')

    def test_creates_client(self):
        client = {
            'id': 'test-client',
        }
        self.fs.create('client', client['id'], client)
        self.assertEqual(self.fs.list('client'), [client['id']])

        gotten = self.fs.get('client', client['id'])
        self.assertEqual(client, gotten)

    def test_updates_client(self):
        client = {
            'id': 'test-client',
        }
        self.fs.create('client', client['id'], client)

        updated = dict(client)
        updated['foo'] = 'bar'
        self.fs.create('client', client['id'], updated)
        self.assertEqual(self.fs.get('client', client['id']), updated)

    def test_two_clients(self):
        one = {
            'id': 'first-client',
        }
        self.fs.create('client', one['id'], one)
        self.assertEqual(self.fs.list('client'), [one['id']])

        two = {
            'id': 'second-client',
        }
        self.fs.create('client', two['id'], two)
        self.assertEqual(self.fs.list('client'), [one['id'], two['id']])

        gotten = self.fs.get('client', two['id'])
        self.assertEqual(two, gotten)

    def test_creates_client_and_user(self):
        client = {
            'id': 'test',
            'type': 'client',
        }

        user = {
            'id': 'test',
            'type': 'user',
        }

        self.fs.create('client', client['id'], client)
        self.fs.create('user', user['id'], user)
        self.assertEqual(self.fs.list('client'), [client['id']])
        self.assertEqual(self.fs.list('user'), [user['id']])

        self.assertEqual(client, self.fs.get('client', client['id']))
        self.assertEqual(user, self.fs.get('user', user['id']))