-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
68 lines (50 loc) · 1.41 KB
/
Copy pathtests.py
File metadata and controls
68 lines (50 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
import os
import random
import string
import pytest
from bddrest import Given, status, when, response, given
from shortener import app
@pytest.fixture
def randommock():
backup = random.randint
random.randint = lambda a, b: 0xF00
yield
random.randint = backup
@pytest.fixture
def redismock():
import shortener
class RedisMock:
def __init__(self):
self.maindict = dict()
def get(self, key):
return self.maindict.get(key, '').encode()
def set(self, key, value):
self.maindict[key] = value
backup = shortener.redis
shortener.redis = RedisMock()
yield shortener.redis
shortener.redis = backup
def test_shortener(randommock, redismock):
with Given(
app,
verb='POST',
json=dict(url='http://example.com')
):
assert status == 201
assert response.text == 'f00'
when(json=dict(url='invalidurl'))
assert status == 400
when(json=given - 'url')
assert status == '400 Field missing: url'
def test_redirector(redismock):
redismock.set('foo', 'https://example.com')
with Given(
app,
url='/foo'
):
assert status == 302
assert response.headers['LOCATION'] == 'https://example.com'
when(url='/notexists')
assert status == 404