tests.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. # Copyright 2013 The Distro Tracker Developers
  2. # See the COPYRIGHT file at the top-level directory of this distribution and
  3. # at http://deb.li/DTAuthors
  4. #
  5. # This file is part of Distro Tracker. It is subject to the license terms
  6. # in the LICENSE file found in the top-level directory of this
  7. # distribution and at http://deb.li/DTLicense. No part of Distro Tracker,
  8. # including this file, may be copied, modified, propagated, or distributed
  9. # except according to the terms contained in the LICENSE file.
  10. """
  11. Tests for the :mod:`distro_tracker.accounts` app.
  12. """
  13. from __future__ import unicode_literals
  14. from distro_tracker.test import TestCase
  15. from distro_tracker.accounts.models import User
  16. from distro_tracker.accounts.models import UserEmail
  17. from distro_tracker.core.models import EmailSettings
  18. from distro_tracker.core.models import PackageName
  19. from distro_tracker.core.models import Subscription
  20. from distro_tracker.core.models import Keyword
  21. from django.core.urlresolvers import reverse
  22. import json
  23. class UserManagerTests(TestCase):
  24. """
  25. Tests for the :class:`distro_tracker.accounts.UserManager` class.
  26. """
  27. def test_create_user(self):
  28. email = 'user@domain.com'
  29. u = User.objects.create_user(main_email=email, password='asdf')
  30. # The user is correctly created
  31. self.assertEqual(1, User.objects.count())
  32. self.assertEqual(email, u.main_email)
  33. self.assertFalse(u.is_superuser)
  34. self.assertFalse(u.is_staff)
  35. self.assertTrue(u.is_active)
  36. # The user is associated with a UserEmail
  37. self.assertEqual(1, u.emails.count())
  38. user_email = UserEmail.objects.all()[0]
  39. self.assertEqual(u, User.objects.get(pk=user_email.user.pk))
  40. def test_create_user_existing_email(self):
  41. """
  42. Tests creating a user when the email already exists.
  43. """
  44. email = 'user@domain.com'
  45. UserEmail.objects.create(email=email)
  46. u = User.objects.create_user(main_email=email, password='asdf')
  47. # The user is associated with the existing email user
  48. self.assertEqual(1, UserEmail.objects.count())
  49. self.assertEqual(
  50. u,
  51. User.objects.get(pk=UserEmail.objects.all()[0].user.pk))
  52. def test_create_superuser(self):
  53. email = 'user@domain.com'
  54. u = User.objects.create_superuser(main_email=email, password='asdf')
  55. # The user is created
  56. self.assertEqual(1, User.objects.count())
  57. self.assertTrue(u.is_superuser)
  58. self.assertTrue(u.is_staff)
  59. def test_create(self):
  60. email = 'user@domain.com'
  61. u = User.objects.create(main_email=email, password='asdf')
  62. # The user is correctly created
  63. self.assertEqual(1, User.objects.count())
  64. self.assertEqual(email, u.main_email)
  65. self.assertFalse(u.is_superuser)
  66. self.assertFalse(u.is_staff)
  67. # This creates inactive users
  68. self.assertFalse(u.is_active)
  69. # The user is associated with a UserEmail
  70. self.assertEqual(1, u.emails.count())
  71. user_email = UserEmail.objects.all()[0]
  72. self.assertEqual(
  73. User.objects.get(pk=u.pk),
  74. User.objects.get(pk=user_email.user.pk))
  75. class UserTests(TestCase):
  76. """
  77. Tests for the :class:`distro_tracker.accounts.User` class.
  78. """
  79. def setUp(self):
  80. self.main_email = 'user@domain.com'
  81. self.user = User.objects.create_user(
  82. main_email=self.main_email, password='asdf')
  83. self.package = PackageName.objects.create(name='dummy-package')
  84. def test_is_subscribed_to_main_email(self):
  85. """
  86. Tests the
  87. :meth:`is_subscribed_to
  88. <distro_tracker.accounts.models.User.is_subscribed_to>`
  89. method when the user is subscribed to the package with his main email
  90. only.
  91. """
  92. email = self.user.emails.all()[0]
  93. Subscription.objects.create_for(
  94. email=email.email,
  95. package_name=self.package.name)
  96. self.assertTrue(self.user.is_subscribed_to(self.package))
  97. self.assertTrue(self.user.is_subscribed_to('dummy-package'))
  98. def test_is_subscribed_to_associated_email(self):
  99. """
  100. Tests the
  101. :meth:`is_subscribed_to
  102. <distro_tracker.accounts.models.User.is_subscribed_to>`
  103. method when the user is subscribed to the package with one of his
  104. associated emails.
  105. """
  106. email = self.user.emails.create(email='other-email@domain.com')
  107. Subscription.objects.create_for(
  108. email=email.email,
  109. package_name=self.package.name)
  110. self.assertTrue(self.user.is_subscribed_to(self.package))
  111. self.assertTrue(self.user.is_subscribed_to('dummy-package'))
  112. def test_is_subscribed_to_all_emails(self):
  113. """
  114. Tests the
  115. :meth:`is_subscribed_to
  116. <distro_tracker.accounts.models.User.is_subscribed_to>`
  117. method when the user is subscribed to the package with all of his
  118. associated emails.
  119. """
  120. self.user.emails.create(email='other-email@domain.com')
  121. for email in self.user.emails.all():
  122. Subscription.objects.create_for(
  123. email=email.email,
  124. package_name=self.package.name)
  125. self.assertTrue(self.user.is_subscribed_to(self.package))
  126. self.assertTrue(self.user.is_subscribed_to('dummy-package'))
  127. def test_is_subscribed_to_on_non_existing_package(self):
  128. self.assertFalse(self.user.is_subscribed_to('does-not-exist'))
  129. def test_unsubscribe_all(self):
  130. """
  131. Test the :meth:`unsubscribe_all
  132. <distro_tracker.accounts.models.User.unsubscribe_all>` method.
  133. """
  134. other_email = 'other-email@domain.com'
  135. self.user.emails.create(email=other_email)
  136. for email in self.user.emails.all():
  137. Subscription.objects.create_for(
  138. email=email.email,
  139. package_name=self.package.name)
  140. self.user.unsubscribe_all()
  141. self.assertEqual(
  142. len(Subscription.objects.get_for_email(self.main_email)),
  143. 0,
  144. 'unsubscribe_all() should remove all subscriptions to main_email')
  145. self.assertEqual(
  146. len(Subscription.objects.get_for_email(other_email)),
  147. 1, 'unsubscribe_all() should not remove other subscriptions')
  148. self.user.unsubscribe_all('other-email@domain.com')
  149. self.assertEqual(
  150. len(Subscription.objects.get_for_email(other_email)), 0,
  151. 'unsubscribe_all(email) should remove all subscriptions of '
  152. ' that email')
  153. class SubscriptionsViewTests(TestCase):
  154. """
  155. Tests the :class:`distro_tracker.accounts.SubscriptionsView`.
  156. """
  157. def setUp(self):
  158. self.package_name = PackageName.objects.create(name='dummy-package')
  159. self.password = 'asdf'
  160. self.user = User.objects.create_user(
  161. main_email='user@domain.com',
  162. password=self.password)
  163. def get_subscriptions_view(self):
  164. self.client.login(username=self.user.main_email, password=self.password)
  165. return self.client.get(reverse('dtracker-accounts-subscriptions'))
  166. def subscribe_email_to_package(self, email, package_name):
  167. Subscription.objects.create_for(
  168. email=email,
  169. package_name=package_name.name)
  170. def test_one_email(self):
  171. """
  172. Tests the scenario where the user only has one email associated with
  173. his account.
  174. """
  175. self.subscribe_email_to_package(self.user.main_email, self.package_name)
  176. response = self.get_subscriptions_view()
  177. self.assertTemplateUsed(response, 'accounts/subscriptions.html')
  178. # The context contains the subscriptions of the user
  179. self.assertIn('subscriptions', response.context)
  180. context_subscriptions = response.context['subscriptions']
  181. email = self.user.emails.all()[0]
  182. # The correct email is in the context
  183. self.assertIn(email, context_subscriptions)
  184. # The packages in the context are correct
  185. self.assertEqual(
  186. [self.package_name.name],
  187. [sub.package.name for sub
  188. in context_subscriptions[email]['subscriptions']])
  189. def test_multiple_emails(self):
  190. """
  191. Tests the scenario where the user has multiple emails associated with
  192. his account.
  193. """
  194. self.user.emails.create(email='other-email@domain.com')
  195. packages = [
  196. self.package_name,
  197. PackageName.objects.create(name='other-package')]
  198. for email, package in zip(self.user.emails.all(), packages):
  199. self.subscribe_email_to_package(email.email, package)
  200. response = self.get_subscriptions_view()
  201. # All the emails are in the context?
  202. context_subscriptions = response.context['subscriptions']
  203. for email, package in zip(self.user.emails.all(), packages):
  204. self.assertIn(email, context_subscriptions)
  205. # Each email has the correct package?
  206. self.assertEqual(
  207. [package.name],
  208. [sub.package.name for sub
  209. in context_subscriptions[email]['subscriptions']])
  210. class UserEmailsViewTests(TestCase):
  211. """
  212. Tests for the :class:`distro_tracker.accounts.views.UserEmailsView` view.
  213. """
  214. def setUp(self):
  215. self.password = 'asdf'
  216. self.user = User.objects.create_user(
  217. main_email='user@domain.com', password=self.password)
  218. def log_in_user(self):
  219. self.client.login(username=self.user.main_email, password=self.password)
  220. def get_emails_view(self):
  221. return self.client.get(reverse('dtracker-api-accounts-emails'))
  222. def test_get_list_of_emails_only_main_email(self):
  223. self.log_in_user()
  224. response = self.get_emails_view()
  225. # The view returns JSON
  226. self.assertEqual('application/json', response['Content-Type'])
  227. # The array contains only the users main email
  228. self.assertEqual(
  229. [email.email for email in self.user.emails.all()],
  230. json.loads(response.content.decode('utf-8')))
  231. def test_user_not_logged_in(self):
  232. """
  233. Tests that when a user is not logged in, no JSON response is given.
  234. """
  235. response = self.get_emails_view()
  236. self.assertNotEqual('application/json', response['Content-Type'])
  237. def test_get_list_of_emails_with_associated_emails(self):
  238. self.user.emails.create(email='other@domain.com')
  239. self.log_in_user()
  240. response = self.get_emails_view()
  241. # The array contains only the users main email
  242. self.assertEqual(
  243. [email.email for email in self.user.emails.all()],
  244. json.loads(response.content.decode('utf-8')))
  245. class SubscribeUserToPackageViewTests(TestCase):
  246. """
  247. Tests for the
  248. :class:`distro_tracker.accounts.views.SubscribeUserToPackageView` view.
  249. """
  250. def setUp(self):
  251. self.password = 'asdf'
  252. self.user = User.objects.create_user(
  253. main_email='user@domain.com', password=self.password)
  254. self.package = PackageName.objects.create(name='dummy-package')
  255. def log_in_user(self):
  256. self.client.login(username=self.user.main_email, password=self.password)
  257. def post_to_view(self, package=None, email=None, ajax=True):
  258. post_params = {}
  259. if package:
  260. post_params['package'] = package
  261. if email:
  262. post_params['email'] = email
  263. kwargs = {}
  264. if ajax:
  265. kwargs = {
  266. 'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest',
  267. }
  268. return self.client.post(
  269. reverse('dtracker-api-accounts-subscribe'), post_params, **kwargs)
  270. def test_subscribe_user(self):
  271. self.log_in_user()
  272. response = self.post_to_view(self.package.name, self.user.main_email)
  273. # After the POST, the user is subscribed to the package?
  274. self.assertTrue(self.user.is_subscribed_to(self.package))
  275. self.assertEqual('application/json', response['Content-Type'])
  276. expected = {
  277. 'status': 'ok'
  278. }
  279. self.assertDictEqual(expected,
  280. json.loads(response.content.decode('utf-8')))
  281. def test_subscribe_user_invalid_package_name_ajax(self):
  282. self.log_in_user()
  283. bad_pkgname = '/../ '
  284. response = self.post_to_view(bad_pkgname, self.user.main_email)
  285. self.assertFalse(self.user.is_subscribed_to(bad_pkgname))
  286. json_data = json.loads(response.content.decode('utf-8'))
  287. self.assertEqual(json_data.get('status'), 'failed')
  288. self.assertTrue(json_data.get('error'))
  289. def test_subscribe_user_invalid_package_name(self):
  290. self.log_in_user()
  291. bad_pkgname = '/../ '
  292. response = self.post_to_view(bad_pkgname, self.user.main_email,
  293. ajax=False)
  294. self.assertFalse(self.user.is_subscribed_to(bad_pkgname))
  295. self.assertEqual(response.status_code, 400)
  296. def test_subscribe_not_logged_in(self):
  297. """
  298. Tests that subscribing does not work when a user is not logged in.
  299. """
  300. self.post_to_view(self.package.name, self.user.main_email)
  301. # The user is not subscribed to the package
  302. self.assertFalse(self.user.is_subscribed_to(self.package))
  303. def test_subscribe_logged_in_not_owner(self):
  304. """
  305. Tests that a logged in user cannot subscribe an email that it does not
  306. own to a package.
  307. """
  308. self.log_in_user()
  309. other_user = User.objects.create_user(
  310. main_email='other@domain.com', password='asdf')
  311. response = self.post_to_view(self.package.name, other_user.main_email)
  312. # The user is not subscribed to the package
  313. self.assertFalse(other_user.is_subscribed_to(self.package))
  314. # Forbidden status code?
  315. self.assertEqual(403, response.status_code)
  316. def test_subscribe_multiple_emails(self):
  317. """
  318. Tests that a user can subscribe multiple emails at once.
  319. """
  320. self.user.emails.create(email='other@domain.com')
  321. self.log_in_user()
  322. self.post_to_view(
  323. email=[e.email for e in self.user.emails.all()],
  324. package=self.package.name)
  325. for email in self.user.emails.all():
  326. self.assertTrue(email.emailsettings.is_subscribed_to(self.package))
  327. def test_subscribe_multiple_emails_does_not_own_one(self):
  328. """
  329. Tests that no subscriptions are created if there is at least one email
  330. that the user does not own in the list of emails.
  331. """
  332. other_email = 'other@domain.com'
  333. UserEmail.objects.create(email=other_email)
  334. emails = [
  335. other_email,
  336. self.user.main_email,
  337. ]
  338. self.log_in_user()
  339. response = self.post_to_view(email=emails, package=self.package.name)
  340. self.assertEqual(403, response.status_code)
  341. class UnsubscribeUserViewTests(TestCase):
  342. """
  343. Tests for the :class:`distro_tracker.accounts.views.UnsubscribeUserView`
  344. view.
  345. """
  346. def setUp(self):
  347. self.package = PackageName.objects.create(name='dummy-package')
  348. self.password = 'asdf'
  349. self.user = User.objects.create_user(
  350. main_email='user@domain.com', password=self.password)
  351. self.user.emails.create(email='other@domain.com')
  352. def subscribe_email_to_package(self, email, package_name):
  353. """
  354. Creates a subscription for the given email and package.
  355. """
  356. Subscription.objects.create_for(
  357. email=email,
  358. package_name=package_name)
  359. def log_in(self):
  360. self.client.login(username=self.user.main_email, password=self.password)
  361. def post_to_view(self, package, email=None, ajax=True):
  362. post_params = {
  363. 'package': package,
  364. }
  365. if email:
  366. post_params['email'] = email
  367. kwargs = {}
  368. if ajax:
  369. kwargs = {
  370. 'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest',
  371. }
  372. return self.client.post(
  373. reverse('dtracker-api-accounts-unsubscribe'), post_params, **kwargs)
  374. def test_unsubscribe_all_emails(self):
  375. """
  376. Tests the scenario where all the user's emails need to be unsubscribed
  377. from the given package.
  378. """
  379. for email in self.user.emails.all():
  380. self.subscribe_email_to_package(email.email, self.package.name)
  381. # Sanity check: the user is subscribed to the package
  382. self.assertTrue(self.user.is_subscribed_to(self.package))
  383. # Make sure the user is logged in
  384. self.log_in()
  385. response = self.post_to_view(package=self.package.name)
  386. # The user is no longer subscribed to the package
  387. self.assertFalse(self.user.is_subscribed_to(self.package))
  388. self.assertEqual('application/json', response['Content-Type'])
  389. def test_unsubscribe_not_logged_in(self):
  390. """
  391. Tests that the user cannot do anything when not logged in.
  392. """
  393. self.subscribe_email_to_package(self.user.main_email, self.package.name)
  394. self.post_to_view(self.package.name)
  395. # The user is still subscribed to the package
  396. self.assertTrue(self.user.is_subscribed_to(self.package))
  397. def test_unsubscribe_one_email(self):
  398. """
  399. Tests the scenario where only one of the user's email should be
  400. unsubscribed from the given package.
  401. """
  402. for email in self.user.emails.all():
  403. self.subscribe_email_to_package(email.email, self.package.name)
  404. # Sanity check: the user is subscribed to the package
  405. self.assertTrue(self.user.is_subscribed_to(self.package))
  406. # Make sure the user is logged in
  407. self.log_in()
  408. self.post_to_view(
  409. package=self.package.name, email=self.user.main_email)
  410. # The user is still considered subscribed to the package
  411. self.assertTrue(self.user.is_subscribed_to(self.package))
  412. # However, the main email is no longer subscribed
  413. for email in self.user.emails.all():
  414. if email.email == self.user.main_email:
  415. self.assertFalse(
  416. email.emailsettings.is_subscribed_to(self.package))
  417. else:
  418. self.assertTrue(
  419. email.emailsettings.is_subscribed_to(self.package))
  420. def test_package_name_not_provided(self):
  421. """
  422. Tests the scenario where the package name is not POSTed.
  423. """
  424. self.log_in()
  425. response = \
  426. self.client.post(reverse('dtracker-api-accounts-unsubscribe'))
  427. self.assertEqual(404, response.status_code)
  428. class UnsubscribeAllViewTests(TestCase):
  429. """
  430. Tests for the :class:`distro_tracker.accounts.views.UnsubscribeAllView`
  431. view.
  432. """
  433. def setUp(self):
  434. self.package = PackageName.objects.create(name='dummy-package')
  435. self.password = 'asdf'
  436. self.user = User.objects.create_user(
  437. main_email='user@domain.com', password=self.password)
  438. self.other_email = self.user.emails.create(email='other@domain.com')
  439. def subscribe_email_to_package(self, email, package_name):
  440. """
  441. Creates a subscription for the given email and package.
  442. """
  443. Subscription.objects.create_for(
  444. email=email,
  445. package_name=package_name)
  446. def log_in(self):
  447. self.client.login(username=self.user.main_email, password=self.password)
  448. def post_to_view(self, email=None, ajax=True):
  449. post_params = {}
  450. if email:
  451. post_params['email'] = email
  452. kwargs = {}
  453. if ajax:
  454. kwargs = {
  455. 'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest',
  456. }
  457. return self.client.post(
  458. reverse('dtracker-api-accounts-unsubscribe-all'), post_params,
  459. **kwargs)
  460. def test_subscriptions_removed(self):
  461. """
  462. Tests that subscriptions are removed for specified emails.
  463. """
  464. all_emails = [
  465. self.user.main_email,
  466. self.other_email.email,
  467. ]
  468. emails_to_unsubscribe = [
  469. self.other_email.email,
  470. ]
  471. other_package = PackageName.objects.create(name='other-package')
  472. for email in self.user.emails.all():
  473. self.subscribe_email_to_package(email.email, self.package.name)
  474. self.subscribe_email_to_package(email.email, other_package.name)
  475. self.log_in()
  476. self.post_to_view(email=emails_to_unsubscribe)
  477. for email in all_emails:
  478. if email in emails_to_unsubscribe:
  479. # These emails no longer have any subscriptions
  480. self.assertEqual(
  481. 0,
  482. Subscription.objects.filter(
  483. email_settings__user_email__email=email).count())
  484. else:
  485. # Otherwise, they have all the subscriptions!
  486. self.assertEqual(
  487. 2,
  488. Subscription.objects.filter(
  489. email_settings__user_email__email=email).count())
  490. def test_user_not_logged_in(self):
  491. """
  492. Tests that nothing is removed when the user is not logged in.
  493. """
  494. for email in self.user.emails.all():
  495. self.subscribe_email_to_package(email.email, self.package.name)
  496. old_subscription_count = Subscription.objects.count()
  497. self.post_to_view(email=self.user.main_email)
  498. self.assertEqual(old_subscription_count, Subscription.objects.count())
  499. class ModifyKeywordsViewTests(TestCase):
  500. """
  501. Tests for the :class:`distro_tracker.accounts.views.ModifyKeywordsView`
  502. view.
  503. """
  504. def setUp(self):
  505. self.package = PackageName.objects.create(name='dummy-package')
  506. self.password = 'asdf'
  507. self.user = User.objects.create_user(
  508. main_email='user@domain.com', password=self.password)
  509. for user_email in self.user.emails.all():
  510. EmailSettings.objects.create(user_email=user_email)
  511. self.other_email = UserEmail.objects.create(user=self.user,
  512. email='other@domain.com')
  513. def subscribe_email_to_package(self, email, package_name):
  514. """
  515. Creates a subscription for the given email and package.
  516. """
  517. return Subscription.objects.create_for(
  518. email=email,
  519. package_name=package_name)
  520. def log_in(self):
  521. self.client.login(username=self.user.main_email, password=self.password)
  522. def post_to_view(self, ajax=True, **post_params):
  523. if 'keyword' in post_params:
  524. post_params['keyword[]'] = post_params['keyword']
  525. del post_params['keyword']
  526. kwargs = {}
  527. if ajax:
  528. kwargs = {
  529. 'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest',
  530. }
  531. return self.client.post(
  532. reverse('dtracker-api-accounts-profile-keywords'), post_params,
  533. **kwargs)
  534. def get_email_keywords(self, email):
  535. user_email = UserEmail.objects.get(email=email)
  536. email_settings, _ = \
  537. EmailSettings.objects.get_or_create(user_email=user_email)
  538. return [keyword.name for keyword
  539. in email_settings.default_keywords.all()]
  540. def default_keywords_equal(self, new_keywords):
  541. default_keywords = self.get_email_keywords(self.user.main_email)
  542. self.assertEqual(len(new_keywords), len(default_keywords))
  543. for new_keyword in new_keywords:
  544. if new_keyword not in default_keywords:
  545. return False
  546. return True
  547. def get_subscription_keywords(self, email, package):
  548. subscription = Subscription.objects.get(
  549. email_settings__user_email__email=email, package__name=package)
  550. return [keyword.name for keyword in subscription.keywords.all()]
  551. def subscription_keywords_equal(self, email, package, new_keywords):
  552. subscription_keywords = self.get_subscription_keywords(email, package)
  553. self.assertEqual(len(new_keywords), len(subscription_keywords))
  554. for new_keyword in new_keywords:
  555. if new_keyword not in subscription_keywords:
  556. return False
  557. return True
  558. def test_modify_default_keywords(self):
  559. """
  560. Tests that a user's default keywords are modified.
  561. """
  562. new_keywords = [keyword.name for keyword in Keyword.objects.all()[:2]]
  563. self.log_in()
  564. self.post_to_view(
  565. email=self.user.main_email,
  566. keyword=new_keywords)
  567. # The email's keywords are changed
  568. self.assertTrue(self.default_keywords_equal(new_keywords))
  569. def test_user_not_logged_in(self):
  570. """
  571. Tests that the user cannot do anything when not logged in.
  572. """
  573. new_keywords = [keyword.name for keyword in Keyword.objects.all()[:2]]
  574. old_keywords = self.get_email_keywords(self.user.main_email)
  575. self.post_to_view(
  576. email=self.user.main_email,
  577. keyword=new_keywords)
  578. self.assertTrue(self.default_keywords_equal(old_keywords))
  579. def test_user_does_not_own_email(self):
  580. """
  581. Tests that when a user does not own the email found in the parameters,
  582. no changes are made.
  583. """
  584. new_email = UserEmail.objects.create(email='new@domain.com')
  585. new_keywords = [keyword.name for keyword in Keyword.objects.all()[:2]]
  586. old_keywords = self.get_email_keywords(new_email.email)
  587. self.log_in()
  588. response = self.post_to_view(
  589. email=new_email.email,
  590. keyword=new_keywords)
  591. self.assertTrue(self.default_keywords_equal(old_keywords))
  592. self.assertEqual(403, response.status_code)
  593. def test_set_subscription_specific_keywords(self):
  594. self.subscribe_email_to_package(
  595. self.user.main_email, self.package.name)
  596. new_keywords = [keyword.name for keyword in Keyword.objects.all()[:2]]
  597. self.log_in()
  598. self.post_to_view(
  599. email=self.user.main_email,
  600. keyword=new_keywords,
  601. package=self.package.name)
  602. self.assertTrue(self.subscription_keywords_equal(
  603. self.user.main_email, self.package.name, new_keywords))
  604. def test_set_subscription_specific_keywords_is_not_owner(self):
  605. """
  606. Tests that the user cannot set keywords for a subscription that it does
  607. not own.
  608. """
  609. new_email = UserEmail.objects.create(email='new@domain.com')
  610. new_keywords = [keyword.name for keyword in Keyword.objects.all()[:2]]
  611. self.subscribe_email_to_package(
  612. new_email.email, self.package.name)
  613. old_keywords = self.get_subscription_keywords(
  614. new_email.email, self.package.name)
  615. self.log_in()
  616. response = self.post_to_view(
  617. email=new_email.email,
  618. keyword=new_keywords,
  619. package=self.package.name)
  620. self.assertTrue(self.subscription_keywords_equal(
  621. new_email.email, self.package.name, old_keywords))
  622. self.assertEqual(403, response.status_code)