-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_test.py
71 lines (61 loc) · 2.45 KB
/
api_test.py
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
69
70
71
"""Affiliate tracking logic"""
import pytest
from django.test.client import RequestFactory
from affiliate.api import (
get_affiliate_code_from_qstring,
get_affiliate_code_from_request,
get_affiliate_id_from_code,
get_affiliate_id_from_request,
)
from affiliate.constants import AFFILIATE_QS_PARAM
from affiliate.factories import AffiliateFactory
def test_get_affiliate_code_from_qstring():
"""
get_affiliate_code_from_qstring should get the affiliate code from the querystring
"""
affiliate_code = "abc"
request = RequestFactory().post(f"/?{AFFILIATE_QS_PARAM}={affiliate_code}", data={})
code = get_affiliate_code_from_qstring(request)
assert code is None
request = RequestFactory().get("/")
code = get_affiliate_code_from_qstring(request)
assert code is None
request = RequestFactory().get(f"/?{AFFILIATE_QS_PARAM}={affiliate_code}")
code = get_affiliate_code_from_qstring(request)
assert code == affiliate_code
def test_get_affiliate_code_from_request():
"""
get_affiliate_code_from_request should get the affiliate code from a request object
"""
affiliate_code = "abc"
request = RequestFactory().get("/")
code = get_affiliate_code_from_request(request)
assert code is None
setattr(request, "affiliate_code", affiliate_code) # noqa: B010
code = get_affiliate_code_from_request(request)
assert code == affiliate_code
@pytest.mark.django_db
def test_get_affiliate_id_from_code():
"""
get_affiliate_id_from_code should fetch the Affiliate id from the database that matches the affiliate code
"""
affiliate_code = "abc"
affiliate_id = get_affiliate_id_from_code(affiliate_code)
assert affiliate_id is None
affiliate = AffiliateFactory.create(code=affiliate_code)
affiliate_id = get_affiliate_id_from_code(affiliate_code)
assert affiliate_id == affiliate.id
@pytest.mark.django_db
def test_get_affiliate_id_from_request():
"""
get_affiliate_id_from_request should fetch the Affiliate id from the database that matches the
affiliate code from the request
"""
affiliate_code = "abc"
request = RequestFactory().get("/")
setattr(request, "affiliate_code", affiliate_code) # noqa: B010
affiliate_id = get_affiliate_id_from_request(request)
assert affiliate_id is None
affiliate = AffiliateFactory.create(code=affiliate_code)
affiliate_id = get_affiliate_id_from_request(request)
assert affiliate_id == affiliate.id