Completed
Push — main ( 25807a...2ed45a )
by Jochen
01:37
created

tests.http.test_receive_server   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 15

10 Functions

Rating   Name   Duplication   Size   Complexity  
A test_server_response_header() 0 10 1
A test_receive_server_without_channel() 0 10 2
A build_request() 0 10 2
A test_receive_server_without_text() 0 10 2
A restricted_server() 0 7 1
A get_server_url() 0 3 1
A test_restricted_access_without_api_token() 0 11 2
A test_restricted_access_with_invalid_api_token() 0 15 2
A test_receive_server_with_valid_request() 0 10 1
A test_restricted_access_with_valid_api_token() 0 14 1
1
"""
2
:Copyright: 2007-2020 Jochen Kupperschmidt
3
:License: MIT, see LICENSE for details.
4
"""
5
6
import json
7
from urllib.error import HTTPError
8
from urllib.request import Request, urlopen
9
10
import pytest
11
12
13
@pytest.fixture
14
def restricted_server(make_server):
15
    api_tokens = {
16
        'gAT3KHqpb94YQ7IMhR-qMH5tRquwLnHoyik_lZItTQY',
17
        'qeV4Jf_PYxAySktCODORTKSH1gs117qJXwoqg5YoDBU',
18
    }
19
    return make_server(api_tokens=api_tokens)
20
21
22
# unrestricted access
23
24
25
def test_receive_server_with_valid_request(server):
26
    data = {
27
        'channel': '#party',
28
        'text': 'Limbo!',
29
    }
30
    request = build_request(server, data)
31
32
    response = urlopen(request)
33
34
    assert response.code == 202
35
36
37
def test_receive_server_without_channel(server):
38
    data = {
39
        'text': 'Which channel is this?!',
40
    }
41
    request = build_request(server, data)
42
43
    with pytest.raises(HTTPError) as excinfo:
44
        urlopen(request)
45
46
    assert excinfo.value.code == 400
47
48
49
def test_receive_server_without_text(server):
50
    data = {
51
        'channel': '#silence',
52
    }
53
    request = build_request(server, data)
54
55
    with pytest.raises(HTTPError) as excinfo:
56
        urlopen(request)
57
58
    assert excinfo.value.code == 400
59
60
61
# restricted access
62
63
64
def test_restricted_access_without_api_token(restricted_server):
65
    data = {
66
        'channel': '#internal',
67
        'text': 'I lost my wallet.',
68
    }
69
    request = build_request(restricted_server, data, api_token=None)
70
71
    with pytest.raises(HTTPError) as excinfo:
72
        urlopen(request)
73
74
    assert excinfo.value.code == 401
75
76
77
def test_restricted_access_with_invalid_api_token(restricted_server):
78
    data = {
79
        'channel': '#internal',
80
        'text': 'I can has access?!',
81
    }
82
    request = build_request(
83
        restricted_server,
84
        data,
85
        api_token='dwNlg-iDnDwx9lPr_DGaZzn2hjHx7_AK2UwUfqsrKr0',
86
    )
87
88
    with pytest.raises(HTTPError) as excinfo:
89
        urlopen(request)
90
91
    assert excinfo.value.code == 403
92
93
94
def test_restricted_access_with_valid_api_token(restricted_server):
95
    data = {
96
        'channel': '#internal',
97
        'text': 'Welcome to the club!',
98
    }
99
    request = build_request(
100
        restricted_server,
101
        data,
102
        api_token='qeV4Jf_PYxAySktCODORTKSH1gs117qJXwoqg5YoDBU',
103
    )
104
105
    response = urlopen(request)
106
107
    assert response.code == 202
108
109
110
# response headers
111
112
113
def test_server_response_header(server):
114
    data = {
115
        'channel': '#curiosity',
116
        'text': 'Show me your web server version!',
117
    }
118
    request = build_request(server, data)
119
120
    response = urlopen(request)
121
122
    assert response.headers['Server'] == 'Weitersager'
123
124
125
# helpers
126
127
128
def build_request(server, data, *, api_token=None):
129
    url = get_server_url(server)
130
    json_data = json.dumps(data).encode('utf-8')
131
132
    request = Request(url, data=json_data, method='POST')
133
134
    if api_token:
135
        request.add_header('Authorization', f'WTRSGR {api_token}')
136
137
    return request
138
139
140
def get_server_url(server):
141
    server_host, server_port = server.server_address
142
    return f'http://{server_host}:{server_port}/'
143