test_auth.py - toot - Unnamed repository; edit this file 'description' to name the repository.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
test_auth.py (1858B)
---
1 # -*- coding: utf-8 -*-
2
3 from toot import App, User, api, config, auth
4 from tests.utils import retval
5
6
7 def test_register_app(monkeypatch):
8 app_data = {'id': 100, 'client_id': 'cid', 'client_secret': 'cs'}
9
10 def assert_app(app):
11 assert isinstance(app, App)
12 assert app.instance == "foo.bar"
13 assert app.base_url == "https://foo.bar"
14 assert app.client_id == "cid"
15 assert app.client_secret == "cs"
16
17 monkeypatch.setattr(api, 'create_app', retval(app_data))
18 monkeypatch.setattr(api, 'get_instance', retval({"title": "foo", "version": "1"}))
19 monkeypatch.setattr(config, 'save_app', assert_app)
20
21 app = auth.register_app("foo.bar")
22 assert_app(app)
23
24
25 def test_create_app_from_config(monkeypatch):
26 """When there is saved config, it's returned"""
27 monkeypatch.setattr(config, 'load_app', retval("loaded app"))
28 app = auth.create_app_interactive("bezdomni.net")
29 assert app == 'loaded app'
30
31
32 def test_create_app_registered(monkeypatch):
33 """When there is no saved config, a new app is registered"""
34 monkeypatch.setattr(config, 'load_app', retval(None))
35 monkeypatch.setattr(auth, 'register_app', retval("registered app"))
36
37 app = auth.create_app_interactive("bezdomni.net")
38 assert app == 'registered app'
39
40
41 def test_create_user(monkeypatch):
42 app = App(4, 5, 6, 7)
43
44 def assert_user(user, activate=True):
45 assert activate
46 assert isinstance(user, User)
47 assert user.instance == app.instance
48 assert user.username == "foo"
49 assert user.access_token == "abc"
50
51 monkeypatch.setattr(config, 'save_user', assert_user)
52 monkeypatch.setattr(api, 'verify_credentials', lambda x, y: {"username": "foo"})
53
54 user = auth.create_user(app, 'abc')
55
56 assert_user(user)
57
58 #
59 # TODO: figure out how to mock input so the rest can be tested
60 #