test_api.py - toot - Unnamed repository; edit this file 'description' to name the repository.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       test_api.py (2096B)
       ---
            1 # -*- coding: utf-8 -*-
            2 import pytest
            3 
            4 from requests import Request
            5 
            6 from toot import App, CLIENT_NAME, CLIENT_WEBSITE
            7 from toot.api import create_app, login, SCOPES, AuthenticationError
            8 from tests.utils import MockResponse, Expectations
            9 
           10 
           11 def test_create_app(monkeypatch):
           12     request = Request('POST', 'https://bigfish.software/api/v1/apps',
           13                       data={'website': CLIENT_WEBSITE,
           14                             'client_name': CLIENT_NAME,
           15                             'scopes': SCOPES,
           16                             'redirect_uris': 'urn:ietf:wg:oauth:2.0:oob'})
           17 
           18     response = MockResponse({'client_id': 'foo',
           19                              'client_secret': 'bar'})
           20 
           21     e = Expectations()
           22     e.add(request, response)
           23     e.patch(monkeypatch)
           24 
           25     create_app('bigfish.software')
           26 
           27 
           28 def test_login(monkeypatch):
           29     app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar')
           30 
           31     data = {
           32         'grant_type': 'password',
           33         'client_id': app.client_id,
           34         'client_secret': app.client_secret,
           35         'username': 'user',
           36         'password': 'pass',
           37         'scope': SCOPES,
           38     }
           39 
           40     request = Request('POST', 'https://bigfish.software/oauth/token', data=data)
           41 
           42     response = MockResponse({
           43         'token_type': 'bearer',
           44         'scope': 'read write follow',
           45         'access_token': 'xxx',
           46         'created_at': 1492523699
           47     })
           48 
           49     e = Expectations()
           50     e.add(request, response)
           51     e.patch(monkeypatch)
           52 
           53     login(app, 'user', 'pass')
           54 
           55 
           56 def test_login_failed(monkeypatch):
           57     app = App('bigfish.software', 'https://bigfish.software', 'foo', 'bar')
           58 
           59     data = {
           60         'grant_type': 'password',
           61         'client_id': app.client_id,
           62         'client_secret': app.client_secret,
           63         'username': 'user',
           64         'password': 'pass',
           65         'scope': SCOPES,
           66     }
           67 
           68     request = Request('POST', 'https://bigfish.software/oauth/token', data=data)
           69     response = MockResponse(is_redirect=True)
           70 
           71     e = Expectations()
           72     e.add(request, response)
           73     e.patch(monkeypatch)
           74 
           75     with pytest.raises(AuthenticationError):
           76         login(app, 'user', 'pass')