|
1
|
|
|
from collections import namedtuple |
|
2
|
|
|
|
|
3
|
|
|
import pytest |
|
4
|
|
|
|
|
5
|
|
|
from besepa.exceptions import ConnectionError, MissingParam, Redirection, ResourceNotFound, UnauthorizedAccess |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
@pytest.fixture |
|
9
|
|
|
def response_fixture(): |
|
10
|
|
|
return namedtuple('Response', 'status_code reason') |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class TestExceptions(object): |
|
14
|
|
|
|
|
15
|
|
|
def test_connection(self): |
|
16
|
|
|
error = ConnectionError({}, 'Test') |
|
17
|
|
|
assert str(error) == "Failed. Error message: Test" |
|
18
|
|
|
|
|
19
|
|
|
def test_redirect(self): |
|
20
|
|
|
error = Redirection({"Location": "http://example.com"}) |
|
21
|
|
|
assert str(error) == "Failed. => http://example.com" |
|
22
|
|
|
|
|
23
|
|
|
def test_not_found(self, response_fixture): |
|
24
|
|
|
response = response_fixture(status_code="404", reason="Not Found") |
|
25
|
|
|
error = ResourceNotFound(response) |
|
26
|
|
|
assert str(error) == "Failed. Response status: %s. Response message: %s." % ( |
|
27
|
|
|
response.status_code, response.reason) |
|
28
|
|
|
|
|
29
|
|
|
def test_unauthorized_access(self, response_fixture): |
|
30
|
|
|
response = response_fixture(status_code="401", reason="Unauthorized") |
|
31
|
|
|
error = UnauthorizedAccess(response) |
|
32
|
|
|
assert str(error) == "Failed. Response status: %s. Response message: %s." % ( |
|
33
|
|
|
response.status_code, response.reason) |
|
34
|
|
|
|
|
35
|
|
|
def test_missing_param(self): |
|
36
|
|
|
error = MissingParam("Missing Payment Id") |
|
37
|
|
|
assert str(error) == "Missing Payment Id" |
|
38
|
|
|
|
|
39
|
|
|
def test_missing_config(self): |
|
40
|
|
|
error = MissingParam("Missing api_key") |
|
41
|
|
|
assert str(error) == "Missing api_key" |
|
42
|
|
|
|