1
+ from rest_framework import status
2
+ from rest_framework .test import APITestCase
3
+
4
+ from rest_framework_jwt .settings import api_settings
5
+
6
+ payload_handler = api_settings .JWT_PAYLOAD_HANDLER
7
+ encode_handler = api_settings .JWT_ENCODE_HANDLER
8
+
9
+ from django .contrib .auth import get_user_model
10
+ from rest_framework .reverse import reverse as api_reverse
11
+
12
+ # automated
13
+ # new / blank db
14
+
15
+ from postings .models import BlogPost
16
+ User = get_user_model ()
17
+
18
+ class BlogPostAPITestCase (APITestCase ):
19
+ def setUp (self ):
20
+ user_obj = User (
username = 'testcfeuser' ,
email = '[email protected] ' )
21
+ user_obj .set_password ("somerandopassword" )
22
+ user_obj .save ()
23
+ blog_post = BlogPost .objects .create (
24
+ user = user_obj ,
25
+ title = 'New title' ,
26
+ content = 'some_random_content'
27
+ )
28
+
29
+
30
+ def test_single_user (self ):
31
+ user_count = User .objects .count ()
32
+ self .assertEqual (user_count , 1 )
33
+
34
+ def test_single_post (self ):
35
+ post_count = BlogPost .objects .count ()
36
+ self .assertEqual (post_count , 1 )
37
+
38
+ def test_get_list (self ):
39
+ # test the get list
40
+ data = {}
41
+ url = api_reverse ("api-postings:post-listcreate" )
42
+ response = self .client .get (url , data , format = 'json' )
43
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
44
+ # print(response.data)
45
+
46
+ def test_post_item (self ):
47
+ # test the get list
48
+ data = {"title" : "Some rando title" , "content" : "some more content" }
49
+ url = api_reverse ("api-postings:post-listcreate" )
50
+ response = self .client .post (url , data , format = 'json' )
51
+ self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
52
+
53
+
54
+ def test_get_item (self ):
55
+ # test the get list
56
+ blog_post = BlogPost .objects .first ()
57
+ data = {}
58
+ url = blog_post .get_api_url ()
59
+ response = self .client .get (url , data , format = 'json' )
60
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
61
+
62
+ def test_update_item (self ):
63
+ # test the get list
64
+ blog_post = BlogPost .objects .first ()
65
+ url = blog_post .get_api_url ()
66
+ data = {"title" : "Some rando title" , "content" : "some more content" }
67
+ response = self .client .post (url , data , format = 'json' )
68
+ self .assertEqual (response .status_code , status .HTTP_405_METHOD_NOT_ALLOWED )
69
+ response = self .client .put (url , data , format = 'json' )
70
+ self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
71
+
72
+
73
+ def test_update_item_with_user (self ):
74
+ # test the get list
75
+ blog_post = BlogPost .objects .first ()
76
+ #print(blog_post.content)
77
+ url = blog_post .get_api_url ()
78
+ data = {"title" : "Some rando title" , "content" : "some more content" }
79
+ user_obj = User .objects .first ()
80
+ payload = payload_handler (user_obj )
81
+ token_rsp = encode_handler (payload )
82
+ self .client .credentials (HTTP_AUTHORIZATION = 'JWT ' + token_rsp ) # JWT <token>
83
+ response = self .client .put (url , data , format = 'json' )
84
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
85
+ #print(response.data)
86
+
87
+ def test_post_item_with_user (self ):
88
+ # test the get list
89
+ user_obj = User .objects .first ()
90
+ payload = payload_handler (user_obj )
91
+ token_rsp = encode_handler (payload )
92
+ self .client .credentials (HTTP_AUTHORIZATION = 'JWT ' + token_rsp )
93
+ data = {"title" : "Some rando title" , "content" : "some more content" }
94
+ url = api_reverse ("api-postings:post-listcreate" )
95
+ response = self .client .post (url , data , format = 'json' )
96
+ self .assertEqual (response .status_code , status .HTTP_201_CREATED )
97
+
98
+
99
+ def test_user_ownership (self ):
100
+ # test the get list
101
+ owner = User .objects .create (username = 'testuser22222' )
102
+ blog_post = BlogPost .objects .create (
103
+ user = owner ,
104
+ title = 'New title' ,
105
+ content = 'some_random_content'
106
+ )
107
+
108
+ user_obj = User .objects .first ()
109
+ self .assertNotEqual (user_obj .username , owner .username )
110
+ payload = payload_handler (user_obj )
111
+ token_rsp = encode_handler (payload )
112
+ self .client .credentials (HTTP_AUTHORIZATION = 'JWT ' + token_rsp )
113
+ url = blog_post .get_api_url ()
114
+ data = {"title" : "Some rando title" , "content" : "some more content" }
115
+ response = self .client .put (url , data , format = 'json' )
116
+ self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
117
+
118
+ def test_user_login_and_update (self ):
119
+ data = {
120
+ 'username' : 'testcfeuser' ,
121
+ 'password' : 'somerandopassword'
122
+ }
123
+ url = api_reverse ("api-login" )
124
+ response = self .client .post (url , data )
125
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
126
+ token = response .data .get ("token" )
127
+ if token is not None :
128
+ blog_post = BlogPost .objects .first ()
129
+ #print(blog_post.content)
130
+ url = blog_post .get_api_url ()
131
+ data = {"title" : "Some rando title" , "content" : "some more content" }
132
+ self .client .credentials (HTTP_AUTHORIZATION = 'JWT ' + token ) # JWT <token>
133
+ response = self .client .put (url , data , format = 'json' )
134
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
135
+
136
+
137
+
138
+
139
+
140
+
141
+ # request.post(url, data, headers={"Authorization": "JWT " + <token> })
0 commit comments