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

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

# -*- coding: utf-8 -*- 

from __future__ import unicode_literals 

 

import unittest 

# import unittest 

from unittest.mock import patch 

 

from django.test import Client 

from django.urls import reverse 

 

from account.helpers.user_auth import UserAuth 

from tweets.helpers.tweet_logic import TweetAll 

 

from .test_helpers import InsertResult 

 

 

# Create your tests here. 

class TweetAPITests(unittest.TestCase): 

''' 

Testing the controller functions only 

''' 

@classmethod 

def setUpClass(self): 

res = {} 

res['message'] = {"Success": "mocking"} 

res['code'] = 200 

auth_res = {} 

auth_res['code'] = 200 

auth_res['user_id'] = 'someid' 

self.client = Client() 

self.res = res 

self.auth_res = auth_res 

# print("\n" + '\x1b[0;34;40m' + 'Starting API tests...' + '\x1b[0m') 

print("\n Starting Tweet API tests...\n") 

 

def setUp(self): 

pass 

 

def tearDown(self): 

pass 

 

@classmethod 

def tearDownClass(self): 

print("Finished Tweet API tests...\n") 

 

@patch('account.helpers.user_auth.UserAuth.check_auth') 

@patch('tweets.helpers.tweet_logic.TweetAll.create') 

def test_01_create_tweet(self, mock_create, mock_auth): 

''' 

Test the login route 

''' 

mock_create.return_value = self.res 

mock_auth.return_value = self.auth_res 

res1 = self.client.get(reverse('create')) 

res2 = self.client.post(reverse('create')) 

self.assertEqual(res1.status_code, 405) 

self.assertEqual(res2.status_code, 200) 

 

@patch('account.helpers.user_auth.UserAuth.check_auth') 

@patch('tweets.helpers.tweet_logic.TweetAll.make_emotion') 

def test_02_emotion_tweet(self, mock_emotion, mock_auth): 

''' 

Test the emotion route 

''' 

mock_emotion.return_value = self.res 

mock_auth.return_value = self.auth_res 

res1 = self.client.get(reverse('emotion', kwargs={"tweet_id":"asdas"})) 

res2 = self.client.post(reverse('emotion', kwargs={"tweet_id":"asdas"})) 

self.assertEqual(res1.status_code, 405) 

self.assertEqual(res2.status_code, 200) 

 

@patch('account.helpers.user_auth.UserAuth.check_auth') 

@patch('tweets.helpers.tweet_logic.TweetAll.retweet') 

def test_03_retweet_tweet(self, mock_retweet, mock_auth): 

''' 

Test the retweet route 

''' 

mock_retweet.return_value = self.res 

mock_auth.return_value = self.auth_res 

res1 = self.client.get(reverse('retweet', kwargs={"tweet_id":"asdas"})) 

res2 = self.client.post(reverse('retweet', kwargs={"tweet_id":"asdas"})) 

self.assertEqual(res1.status_code, 405) 

self.assertEqual(res2.status_code, 200) 

 

@patch('account.helpers.user_auth.UserAuth.check_auth') 

@patch('tweets.helpers.tweet_logic.TweetAll.delete') 

@patch('tweets.helpers.tweet_logic.TweetAll.view') 

def test_04_handle_tweet(self, mock_view, mock_delete, mock_auth): 

''' 

Test the handle view/delete route 

''' 

mock_delete.return_value = self.res 

mock_view.return_value = self.res 

mock_auth.return_value = self.auth_res 

res1 = self.client.get(reverse('handle', kwargs={"tweet_id":"asdas"})) 

res2 = self.client.delete(reverse('handle', kwargs={"tweet_id":"asdas"})) 

self.assertEqual(res1.status_code, 200) 

self.assertEqual(res2.status_code, 200) 

 

@patch('account.helpers.user_auth.UserAuth.check_auth') 

@patch('tweets.helpers.tweet_logic.TweetAll.reply') 

def test_05_handle_reply(self, mock_reply, mock_auth): 

''' 

Test the reply route 

''' 

mock_reply.return_value = self.res 

mock_auth.return_value = self.auth_res 

res1 = self.client.get(reverse('reply', kwargs={"tweet_id":"asdas"})) 

res2 = self.client.post(reverse('reply', kwargs={"tweet_id":"asdas"})) 

self.assertEqual(res1.status_code, 405) 

self.assertEqual(res2.status_code, 200) 

 

@patch('account.helpers.user_auth.UserAuth.check_auth') 

@patch('tweets.helpers.tweet_logic.TweetAll.thread') 

def test_05_handle_thread(self, mock_thread, mock_auth): 

''' 

Test the thread route 

''' 

mock_thread.return_value = self.res 

mock_auth.return_value = self.auth_res 

res1 = self.client.get(reverse('thread')) 

res2 = self.client.post(reverse('thread')) 

self.assertEqual(res1.status_code, 400) 

self.assertEqual(res2.status_code, 200) 

 

@patch('account.helpers.user_auth.UserAuth.check_auth') 

@patch('tweets.helpers.tweet_logic.TweetAll.thread_view') 

def test_06_view_thread(self, mock_thread, mock_auth): 

''' 

Test the thread route 

''' 

mock_thread.return_value = self.res 

mock_auth.return_value = self.auth_res 

res1 = self.client.get(reverse('view_thread', kwargs={"thread_id":"asdas"})) 

self.assertEqual(res1.status_code, 200) 

 

class TweetHandlerTests(unittest.TestCase): 

''' 

Testing the controller functions only 

''' 

@classmethod 

def setUpClass(self): 

res = {} 

res['message'] = {"Success": "mocking"} 

res['code'] = 200 

auth_res = {} 

auth_res['code'] = 200 

auth_res['user_id'] = 'someid' 

self.client = Client() 

self.res = res 

self.auth_res = auth_res 

self.tweet_all = TweetAll() 

# print("\n" + '\x1b[0;34;40m' + 'Starting API tests...' + '\x1b[0m') 

print("\n Starting Tweet Handler tests...\n") 

 

def setUp(self): 

pass 

 

def tearDown(self): 

pass 

 

@classmethod 

def tearDownClass(self): 

print("Finished Tweet Handler tests...\n") 

 

@patch('pymongo.collection.Collection.insert_one') 

def test_01_create(self, mocked_insert): 

user_id = 'abcd' 

tweet_text = ['', 'not'] 

ins = InsertResult() 

mocked_insert.return_value = ins 

res1 = self.tweet_all.create(user_id, tweet_text[0]) 

res2 = self.tweet_all.create(user_id, tweet_text[1]) 

self.assertEqual(res1['code'], 400) 

self.assertEqual(res1['code'], 400) 

 

@patch('pymongo.collection.Collection.find_one') 

@patch('pymongo.collection.Collection.delete_one') 

def test_02_delete(self, mocked_delete, mocked_find): 

user_id = 'abcd' 

tweet_id = ['', 'not'] 

mocked_find.return_value = { 

"_id": "abcd", 

"user_id": "abcd", 

"is_retweet": False, 

"is_reply": False 

} 

mocked_delete.return_value = 'asa' 

res1 = self.tweet_all.delete(user_id, tweet_id[0]) 

res2 = self.tweet_all.delete(user_id, tweet_id[1]) 

self.assertEqual(res1['code'], 400) 

self.assertEqual(res2['code'], 200) 

 

@patch('pymongo.collection.Collection.find_one') 

@patch('pymongo.collection.Collection.delete_one') 

def test_03_view(self, mocked_delete, mocked_find): 

user_id = 'abcd' 

tweet_id = ['', 'not'] 

mocked_find.return_value = { 

"_id": "abcd", 

"is_retweet": False 

} 

mocked_delete.return_value = 'asa' 

res1 = self.tweet_all.view(tweet_id[0]) 

res2 = self.tweet_all.view(tweet_id[1]) 

self.assertEqual(res1['code'], 400) 

self.assertEqual(res2['code'], 200) 

 

@patch('pymongo.collection.Collection.find_one') 

@patch('pymongo.collection.Collection.update_one') 

def test_04_emotion(self, mocked_update, mocked_find): 

user_id = "abcd" 

tweet_id = ["", "not"] 

mocked_find.return_value = { 

"_id": "abcd", 

"fav_list": [ ] 

} 

mocked_update.return_value = 'asa' 

res1 = self.tweet_all.make_emotion(user_id, tweet_id[0]) 

res2 = self.tweet_all.make_emotion(user_id, tweet_id[1]) 

self.assertEqual(res1['code'], 400) 

self.assertEqual(res2['code'], 200) 

 

@patch('pymongo.collection.Collection.find_one') 

@patch('pymongo.collection.Collection.insert_one') 

@patch('pymongo.collection.Collection.update_one') 

def test_05_retweet(self, mocked_update, mocked_insert, mocked_find): 

user_id = 'abcd' 

tweet_id = ['', 'not'] 

mocked_find.return_value = { 

"_id": "abcd", 

"is_retweet": False, 

"fav_list": [], 

"user_id": 'mock_id', 

"tweet_text": "mock" 

} 

mocked_insert.return_value = 'asa' 

mocked_update.return_value = 'asa' 

res1 = self.tweet_all.retweet(user_id, tweet_id[0]) 

res2 = self.tweet_all.retweet(user_id, tweet_id[1]) 

self.assertEqual(res1['code'], 400) 

self.assertEqual(res2['code'], 500) 

 

@patch('pymongo.collection.Collection.find_one') 

@patch('pymongo.collection.Collection.insert_one') 

@patch('pymongo.collection.Collection.update_one') 

def test_06_reply(self, mocked_update, mocked_insert, mocked_find): 

user_id = 'abcd' 

tweet_id = ['', 'not'] 

mocked_find.return_value = { 

"_id": "abcd", 

"is_retweet": False, 

"fav_list": [], 

"user_id": 'mock_id', 

"tweet_text": "mock" 

} 

mocked_insert.return_value = 'asa' 

mocked_update.return_value = 'asa' 

reply_text = "reply" 

res1 = self.tweet_all.reply(user_id, tweet_id[0], reply_text) 

res2 = self.tweet_all.reply(user_id, tweet_id[1], reply_text) 

self.assertEqual(res1['code'], 400) 

self.assertEqual(res2['code'], 200) 

 

@patch('pymongo.collection.Collection.insert_many') 

def test_07_thread(self, mocked_insert): 

user_id = 'abcd' 

tweet_id = ['', 'not'] 

ins = InsertResult() 

mocked_insert.return_value = ins 

thread_texts = [[], ["thread"]] 

res1 = self.tweet_all.thread(user_id, thread_texts[0]) 

res2 = self.tweet_all.thread(user_id, thread_texts[1]) 

self.assertEqual(res1['code'], 400) 

self.assertEqual(res2['code'], 200) 

 

@patch('pymongo.collection.Collection.find') 

def test_08_thread_view(self, mocked_find): 

thread_id = ['', 'not'] 

mocked_find.return_value = {} 

res1 = self.tweet_all.thread_view(thread_id[0]) 

res2 = self.tweet_all.thread_view(thread_id[1]) 

self.assertEqual(res1['code'], 400) 

self.assertEqual(res2['code'], 200)