Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

import json 

from unittest import mock 

 

from django.test import TestCase, Client 

from django.urls import reverse 

 

from common.models import Fanfic, Chapter, Reading 

from users.models import CustomUser 

 

 

class ChaptersNotesViewTests(TestCase): 

 

def setUp(self): 

self.normal_user = CustomUser.objects.create( 

name_surname="name", 

country="AM", 

date_of_birth="2000-07-02", 

email="em@gm.com", 

username='testuser', 

password="12345") 

 

self.fanfic = Fanfic.objects.create(name="Testek fanfic", 

author="michaelRuiz", 

web="http://web-fanfic.com", 

genre1="adv", 

complete=True) 

self.chapter = Chapter.objects.create(title="Whatever", num_chapter=1, 

url_chapter="url", 

fanfic=self.fanfic) 

 

self.reading = Reading.objects.create(chapter=self.chapter, 

user=self.normal_user, 

read=False) 

 

self.client = Client() 

self.client.force_login(user=self.normal_user) 

 

def test_delete_private_notes(self): 

""" Test clear private notes of user for one fanfic""" 

response = self.client.delete(reverse( 

'chapters_fanfics:chapters_notes', 

kwargs={ 

'fanfic_id': self.fanfic.id}), 

follow=True) 

 

self.assertEqual(response.status_code, 200) 

self.assertTemplateUsed(response, 'fanfic.html') 

 

readings = Reading.objects.filter(chapter__fanfic=self.fanfic, 

user=self.normal_user, 

private_notes__isnull=False).count() 

 

self.assertEqual(readings, 0) 

 

@mock.patch('django.core.handlers.wsgi.WSGIRequest.is_ajax') 

def test_create_edit_private_note(self, mock_is_ajax): 

""" Test Create/edit message""" 

mock_is_ajax.return_value = True 

response = self.client.post(reverse( 

'chapters_fanfics:chapters_note', 

kwargs={ 

'fanfic_id': self.fanfic.id, 

'chapter_id': self.chapter.id} 

), {"text": "this is a " 

"test " 

"private note"}) 

 

self.assertEqual(response.status_code, 200) 

response_json = json.loads(response.content) 

self.assertEqual(response_json['success'], True) 

 

response = self.client.post(reverse( 

'chapters_fanfics:chapters_note', 

kwargs={ 

'fanfic_id': self.fanfic.id, 

'chapter_id': self.chapter.id} 

), {"text": "this is a " 

"test " 

"private note"}) 

 

self.assertEqual(response.status_code, 200) 

response_json = json.loads(response.content) 

self.assertEqual(response_json['success'], True) 

 

@mock.patch('django.core.handlers.wsgi.WSGIRequest.is_ajax') 

def test_create_edit_private_note_empty(self, mock_is_ajax): 

""" Test Create/edit message""" 

mock_is_ajax.return_value = True 

response = self.client.post(reverse( 

'chapters_fanfics:chapters_note', 

kwargs={ 

'fanfic_id': self.fanfic.id, 

'chapter_id': self.chapter.id} 

), {"text": ""}) 

 

self.assertEqual(response.status_code, 200) 

response_json = json.loads(response.content) 

self.assertEqual(response_json['success'], True) 

 

 

class ChaptersViewTests(TestCase): 

 

def setUp(self): 

self.normal_user = CustomUser.objects.create( 

name_surname="name", 

country="AM", 

date_of_birth="2000-07-02", 

email="em@gm.com", 

username='testuser', 

password="12345") 

 

self.fanfic = Fanfic.objects.create(name="Testek fanfic", 

author="michaelRuiz", 

web="http://web-fanfic.com", 

genre1="adv", 

complete=True) 

self.chapter = Chapter.objects.create(title="Whatever", num_chapter=1, 

url_chapter="url", 

fanfic=self.fanfic) 

 

self.client = Client() 

self.client.force_login(user=self.normal_user) 

 

def test_delete_all(self): 

""" Test Clear all the data of one user for one fanfic""" 

response = self.client.delete(reverse( 

'chapters_fanfics:chapters', kwargs={'fanfic_id': self.fanfic.id}), 

follow=True) 

 

self.assertEqual(response.status_code, 200) 

self.assertTemplateUsed(response, 'fanfic.html') 

 

readings = Reading.objects.filter(chapter__fanfic=self.fanfic, 

user=self.normal_user).count() 

 

self.assertEqual(readings, 0) 

 

def test_mark_all_as_read(self): 

""" Test Mark all chapters as read """ 

response = self.client.post(reverse( 

'chapters_fanfics:chapters', 

kwargs={'fanfic_id': self.fanfic.id}), follow=True) 

 

self.assertEqual(response.status_code, 200) 

self.assertTemplateUsed(response, 'fanfic.html') 

 

readings = Reading.objects.filter(chapter__fanfic=self.fanfic, 

user=self.normal_user, 

read=False).count() 

 

self.assertEqual(readings, 0) 

 

@mock.patch('django.core.handlers.wsgi.WSGIRequest.is_ajax') 

def test_mark_chapter_as_read_unread(self, mock_is_ajax): 

""" Test Mark chapter as read/unread """ 

mock_is_ajax.return_value = True 

response = self.client.post(reverse( 

'chapters_fanfics:chapter_mark', 

kwargs={ 

'fanfic_id': self.fanfic.id, 

'chapter_id': self.chapter.id} 

)) 

 

self.assertEqual(response.status_code, 200) 

response_json = json.loads(response.content) 

self.assertEqual(response_json['success'], True) 

 

@mock.patch('django.core.handlers.wsgi.WSGIRequest.is_ajax') 

def test_mark_chapter_as_read(self, mock_is_ajax): 

""" Test Mark last chapter of fanfic and user as read """ 

mock_is_ajax.return_value = True 

response = self.client.post(reverse( 

'chapters_fanfics:chapter_read', 

kwargs={ 

'fanfic_id': self.fanfic.id} 

)) 

 

self.assertEqual(response.status_code, 200) 

response_json = json.loads(response.content) 

self.assertEqual(response_json['success'], True) 

self.assertTrue(Reading.objects.filter(user=self.normal_user, 

chapter=self.chapter, 

read=True).exists())