test_config.py - toot - Unnamed repository; edit this file 'description' to name the repository.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
test_config.py (4879B)
---
1 import os
2 import pytest
3
4 from toot import User, App, config
5
6
7 @pytest.fixture
8 def sample_config():
9 return {
10 'apps': {
11 'foo.social': {
12 'base_url': 'https://foo.social',
13 'client_id': 'abc',
14 'client_secret': 'def',
15 'instance': 'foo.social'
16 },
17 'bar.social': {
18 'base_url': 'https://bar.social',
19 'client_id': 'ghi',
20 'client_secret': 'jkl',
21 'instance': 'bar.social'
22 },
23 },
24 'users': {
25 'foo@bar.social': {
26 'access_token': 'mno',
27 'instance': 'bar.social',
28 'username': 'ihabunek'
29 }
30 },
31 'active_user': 'foo@bar.social',
32 }
33
34
35 def test_extract_active_user_app(sample_config):
36 user, app = config.extract_user_app(sample_config, sample_config['active_user'])
37
38 assert isinstance(user, User)
39 assert user.instance == 'bar.social'
40 assert user.username == 'ihabunek'
41 assert user.access_token == 'mno'
42
43 assert isinstance(app, App)
44 assert app.instance == 'bar.social'
45 assert app.base_url == 'https://bar.social'
46 assert app.client_id == 'ghi'
47 assert app.client_secret == 'jkl'
48
49
50 def test_extract_active_when_no_active_user(sample_config):
51 # When there is no active user
52 assert config.extract_user_app(sample_config, None) == (None, None)
53
54 # When active user does not exist for whatever reason
55 assert config.extract_user_app(sample_config, 'does-not-exist') == (None, None)
56
57 # When active app does not exist for whatever reason
58 sample_config['users']['foo@bar.social']['instance'] = 'does-not-exist'
59 assert config.extract_user_app(sample_config, 'foo@bar.social') == (None, None)
60
61
62 def test_save_app(sample_config):
63 app = App('xxx.yyy', 2, 3, 4)
64 app2 = App('moo.foo', 5, 6, 7)
65
66 app_count = len(sample_config['apps'])
67 assert 'xxx.yyy' not in sample_config['apps']
68 assert 'moo.foo' not in sample_config['apps']
69
70 # Sets
71 config.save_app.__wrapped__(sample_config, app)
72 assert len(sample_config['apps']) == app_count + 1
73 assert 'xxx.yyy' in sample_config['apps']
74 assert sample_config['apps']['xxx.yyy']['instance'] == 'xxx.yyy'
75 assert sample_config['apps']['xxx.yyy']['base_url'] == 2
76 assert sample_config['apps']['xxx.yyy']['client_id'] == 3
77 assert sample_config['apps']['xxx.yyy']['client_secret'] == 4
78
79 # Overwrites
80 config.save_app.__wrapped__(sample_config, app2)
81 assert len(sample_config['apps']) == app_count + 2
82 assert 'xxx.yyy' in sample_config['apps']
83 assert 'moo.foo' in sample_config['apps']
84 assert sample_config['apps']['xxx.yyy']['instance'] == 'xxx.yyy'
85 assert sample_config['apps']['xxx.yyy']['base_url'] == 2
86 assert sample_config['apps']['xxx.yyy']['client_id'] == 3
87 assert sample_config['apps']['xxx.yyy']['client_secret'] == 4
88 assert sample_config['apps']['moo.foo']['instance'] == 'moo.foo'
89 assert sample_config['apps']['moo.foo']['base_url'] == 5
90 assert sample_config['apps']['moo.foo']['client_id'] == 6
91 assert sample_config['apps']['moo.foo']['client_secret'] == 7
92
93 # Idempotent
94 config.save_app.__wrapped__(sample_config, app2)
95 assert len(sample_config['apps']) == app_count + 2
96 assert 'xxx.yyy' in sample_config['apps']
97 assert 'moo.foo' in sample_config['apps']
98 assert sample_config['apps']['xxx.yyy']['instance'] == 'xxx.yyy'
99 assert sample_config['apps']['xxx.yyy']['base_url'] == 2
100 assert sample_config['apps']['xxx.yyy']['client_id'] == 3
101 assert sample_config['apps']['xxx.yyy']['client_secret'] == 4
102 assert sample_config['apps']['moo.foo']['instance'] == 'moo.foo'
103 assert sample_config['apps']['moo.foo']['base_url'] == 5
104 assert sample_config['apps']['moo.foo']['client_id'] == 6
105 assert sample_config['apps']['moo.foo']['client_secret'] == 7
106
107
108 def test_delete_app(sample_config):
109 app = App('foo.social', 2, 3, 4)
110
111 app_count = len(sample_config['apps'])
112
113 assert 'foo.social' in sample_config['apps']
114
115 config.delete_app.__wrapped__(sample_config, app)
116 assert 'foo.social' not in sample_config['apps']
117 assert len(sample_config['apps']) == app_count - 1
118
119 # Idempotent
120 config.delete_app.__wrapped__(sample_config, app)
121 assert 'foo.social' not in sample_config['apps']
122 assert len(sample_config['apps']) == app_count - 1
123
124
125 def test_get_config_file_path():
126 fn = config.get_config_file_path
127
128 os.unsetenv('XDG_CONFIG_HOME')
129
130 assert fn() == os.path.expanduser('~/.config/toot/config.json')
131
132 os.environ['XDG_CONFIG_HOME'] = '/foo/bar/config'
133
134 assert fn() == '/foo/bar/config/toot/config.json'
135
136 os.environ['XDG_CONFIG_HOME'] = '~/foo/config'
137
138 assert fn() == os.path.expanduser('~/foo/config/toot/config.json')