1
1
from django .test import TestCase
2
2
from rest_framework .test import APIClient , APITestCase
3
3
from accounts .models import User
4
- from challenges .models import Challenge
4
+ from challenges .models import Challenge , Tag , Attachment , Source
5
5
import sure
6
6
import json
7
+ import pdb
7
8
8
9
class ChallengeApiTests (APITestCase ):
9
10
def setUp (self ):
10
11
self .client = APIClient ()
11
12
self .content_type = 'application/vnd.api+json'
12
13
13
- def test_challenge_create (self ):
14
- """
15
- Ensure we get the correct challenge
16
- """
17
- user = User .objects .filter (username = "codecorgi" ).first ()
14
+ def get_sample_challenge (self , user = None , tags = None ):
15
+ if user is None :
16
+ user = User .objects .filter (username = "codecorgi" ).first ()
18
17
19
- challenge = Challenge .objects .create (user = user ,
18
+ challenge = Challenge .objects .create (
19
+ user = user ,
20
20
title = 'Test Challenge' ,
21
21
short_title = 'Test Challenge' ,
22
22
owner = 'Gandalf' ,
@@ -31,12 +31,65 @@ def test_challenge_create(self):
31
31
code_tips = 'Use a linter' ,
32
32
)
33
33
34
+ if not tags is None :
35
+ challenge .tags .set (tags )
36
+
37
+ return challenge
38
+
39
+
40
+ def test_challenge_create (self ):
41
+ """
42
+ Ensure we get the correct challenge
43
+ """
44
+ challenge = self .get_sample_challenge ()
45
+
34
46
response = self .client .get (f'/services/api/challenges?pk={ challenge .id } ' ,
35
47
content_type = self .content_type )
36
48
37
49
response .status_code .should .equal (200 )
38
50
39
51
response_data = json .loads (response .content )
40
52
attributes = response_data ['data' ][0 ]['attributes' ]
41
- relationships = response_data ['data' ][0 ]['relationships' ]
42
53
attributes ['title' ].should .equal (challenge .title )
54
+
55
+ def test_challenge_create_with_tags (self ):
56
+ """
57
+ Ensure we get the correct challenge
58
+ """
59
+ tag1 = Tag .objects .create (name = 'Javascript' )
60
+
61
+ challenge = self .get_sample_challenge (tags = [ tag1 ])
62
+
63
+ response = self .client .get (f'/services/api/challenges?pk={ challenge .id } ' ,
64
+ content_type = self .content_type )
65
+
66
+ response .status_code .should .equal (200 )
67
+
68
+ response_data = json .loads (response .content )
69
+ attributes = response_data ['data' ][0 ]['attributes' ]
70
+ included = response_data ['included' ]
71
+
72
+ attributes ['title' ].should .equal (challenge .title )
73
+
74
+ [item for item in included if item .get ('type' ) == 'tags' ][0 ]['attributes' ]['name' ].should .equal (tag1 .name )
75
+
76
+ def test_challenge_create_with_source (self ):
77
+ """
78
+ Ensure we get the correct challenge
79
+ """
80
+ challenge = self .get_sample_challenge ()
81
+ source = Source .objects .create (
82
+ challenge = challenge ,
83
+ name = 'github' ,
84
+ url = 'https://github.com/corgicode'
85
+ )
86
+
87
+ response = self .client .get (f'/services/api/challenges?pk={ challenge .id } ' ,
88
+ content_type = self .content_type )
89
+
90
+ response .status_code .should .equal (200 )
91
+
92
+ response_data = json .loads (response .content )
93
+ included = response_data ['included' ]
94
+
95
+ [item for item in included if item .get ('type' ) == 'sources' ][0 ]['attributes' ]['name' ].should .equal (source .name )
0 commit comments