1
1
import pytest
2
2
from django .contrib .auth import get_user_model
3
+ from rest_framework .test import APIClient
3
4
4
- from sponsor .models import SponsorLevel
5
+ from sponsor .models import Patron , SponsorLevel
5
6
6
7
pytestmark = pytest .mark .django_db
7
8
8
9
UserModel = get_user_model ()
9
10
10
11
12
+ client = APIClient ()
13
+
14
+
11
15
@pytest .mark .django_db
12
16
class TestSponsorLevelModel :
13
17
pytestmark = pytest .mark .django_db
@@ -23,4 +27,70 @@ def test_sponsor_level_creation_success(self):
23
27
assert SponsorLevel .objects .count () != 0
24
28
25
29
26
- # Create your tests here.
30
+ @pytest .mark .django_db
31
+ class TestPatron :
32
+ pytestmark = pytest .mark .django_db
33
+
34
+ def test_patron_list_api (self ):
35
+ assert Patron .objects .count () == 0
36
+ response = client .get ("/sponsors/patron/list/" , format = "json" )
37
+ assert response .status_code == 200
38
+ assert len (response .data ) == 0
39
+
40
+ Patron .objects .create (
41
+ name = "Python Lover 1" ,
42
+ contribution_message = "I love Python" ,
43
+ total_contribution = 1000000 ,
44
+ contribution_datetime = "2023-07-27 00:00:00+09:00" ,
45
+ )
46
+ assert Patron .objects .count () == 1
47
+ response = client .get ("/sponsors/patron/list/" , format = "json" )
48
+ assert response .status_code == 200
49
+ assert len (response .data ) == 1
50
+ assert response .data [0 ]["name" ] == "Python Lover 1"
51
+ # check sort order
52
+ assert response .data [0 ]["sort_order" ] == 1
53
+
54
+ # add second patron
55
+ Patron .objects .create (
56
+ name = "Python Lover 2" ,
57
+ contribution_message = "I love Python too" ,
58
+ total_contribution = 1000001 ,
59
+ contribution_datetime = "2023-07-27 00:00:00+09:00" ,
60
+ )
61
+ assert Patron .objects .count () == 2
62
+ response = client .get ("/sponsors/patron/list/" , format = "json" )
63
+ assert response .status_code == 200
64
+ assert len (response .data ) == 2
65
+ assert response .data [0 ]["name" ] == "Python Lover 2"
66
+ # check sort order
67
+ assert response .data [0 ]["sort_order" ] == 1
68
+ assert response .data [1 ]["name" ] == "Python Lover 1"
69
+ assert response .data [1 ]["sort_order" ] == 2
70
+
71
+ # add third patron
72
+ # check contribution_datetime is earlier than Python Lover 2
73
+ Patron .objects .create (
74
+ name = "Python Lover 3" ,
75
+ contribution_message = "I love Python most" ,
76
+ total_contribution = 1000001 ,
77
+ # earlier contribution then Python Lover 2
78
+ contribution_datetime = "2023-07-26 00:00:00+09:00" ,
79
+ )
80
+ assert Patron .objects .count () == 3
81
+ response = client .get ("/sponsors/patron/list/" , format = "json" )
82
+ assert response .status_code == 200
83
+ assert len (response .data ) == 3
84
+ assert response .data [0 ]["name" ] == "Python Lover 3"
85
+ # check sort order
86
+ assert response .data [0 ]["sort_order" ] == 1
87
+ assert response .data [1 ]["name" ] == "Python Lover 2"
88
+ assert response .data [1 ]["sort_order" ] == 2
89
+
90
+ @pytest .skip ("TODO: implement" )
91
+ def test_patron_message_html_sanitizer (self ):
92
+ assert Patron .objects .count () == 0
93
+ # check patron save will sanitize html field
94
+ # allow only <a> tag
95
+ # allow emoji
96
+ pass
0 commit comments