CCrypto VPN public website https://vpn.ccrypto.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

371 lines
13 KiB

  1. from datetime import timedelta
  2. from django.test import TestCase
  3. from django.utils import timezone
  4. from django.core.management import call_command
  5. from django.core import mail
  6. from django.utils.six import StringIO
  7. from constance import config as site_config
  8. from constance.test import override_config
  9. from .forms import SignupForm
  10. from .models import VPNUser, User, random_gift_code, GiftCode, GiftCodeUser
  11. from payments.models import Payment, Subscription
  12. class UserTestMixin:
  13. def assertRemaining(self, vpnuser, time):
  14. """ Check that the vpnuser will expire in time (+/- 5 seconds) """
  15. exp = vpnuser.expiration or timezone.now()
  16. seconds = (exp - timezone.now() - time).total_seconds()
  17. self.assertAlmostEqual(seconds, 0, delta=5)
  18. class UserModelTest(TestCase, UserTestMixin):
  19. def setUp(self):
  20. User.objects.create_user('aaa')
  21. def test_add_time(self):
  22. u = User.objects.get(username='aaa')
  23. vu = u.vpnuser
  24. p = timedelta(days=1)
  25. self.assertFalse(vu.is_paid)
  26. vu.expiration = timezone.now()
  27. vu.add_paid_time(p)
  28. final = vu.expiration
  29. self.assertRemaining(vu, p)
  30. self.assertGreater(final, timezone.now())
  31. self.assertTrue(vu.is_paid)
  32. def test_add_time_past(self):
  33. u = User.objects.get(username='aaa')
  34. vu = u.vpnuser
  35. p = timedelta(days=1)
  36. self.assertFalse(vu.is_paid)
  37. vu.expiration = timezone.now() - timedelta(days=135)
  38. vu.add_paid_time(p)
  39. final = vu.expiration
  40. self.assertRemaining(vu, p)
  41. self.assertGreater(final, timezone.now())
  42. self.assertTrue(vu.is_paid)
  43. def test_add_time_initial(self):
  44. u = User.objects.get(username='aaa')
  45. vu = u.vpnuser
  46. p = timedelta(days=1)
  47. self.assertFalse(vu.is_paid)
  48. vu.add_paid_time(p)
  49. self.assertTrue(vu.is_paid)
  50. def test_paid_between_subscr_payments(self):
  51. u = User.objects.get(username='aaa')
  52. vu = u.vpnuser
  53. s = Subscription(user=u, backend_id='paypal', status='new')
  54. s.save()
  55. self.assertFalse(vu.is_paid)
  56. s.status = 'active'
  57. s.save()
  58. self.assertTrue(vu.is_paid)
  59. def test_grant_trial(self):
  60. p = timedelta(days=1)
  61. u = User.objects.get(username='aaa')
  62. vu = u.vpnuser
  63. with override_config(TRIAL_PERIOD_HOURS=24, TRIAL_PERIOD_MAX=2):
  64. self.assertEqual(vu.remaining_trial_periods, 2)
  65. self.assertTrue(vu.can_have_trial)
  66. vu.give_trial_period()
  67. self.assertRemaining(vu, p)
  68. self.assertEqual(vu.remaining_trial_periods, 1)
  69. self.assertTrue(vu.can_have_trial)
  70. vu.give_trial_period()
  71. self.assertRemaining(vu, p * 2)
  72. self.assertEqual(vu.remaining_trial_periods, 0)
  73. self.assertFalse(vu.can_have_trial)
  74. def test_trial_refused(self):
  75. p = timedelta(days=1)
  76. u = User.objects.get(username='aaa')
  77. payment = Payment.objects.create(user=u, status='confirmed', amount=300,
  78. time=timedelta(days=30))
  79. payment.save()
  80. vu = u.vpnuser
  81. with override_config(TRIAL_PERIOD_HOURS=24, TRIAL_PERIOD_MAX=2):
  82. self.assertEqual(vu.remaining_trial_periods, 2)
  83. self.assertFalse(vu.can_have_trial)
  84. class UserModelReferrerTest(TestCase, UserTestMixin):
  85. def setUp(self):
  86. self.referrer = User.objects.create_user('ref')
  87. self.without_ref = User.objects.create_user('aaaa')
  88. self.with_ref = User.objects.create_user('bbbb')
  89. self.with_ref.vpnuser.referrer = self.referrer
  90. self.payment = Payment.objects.create(
  91. user=self.with_ref, status='confirmed', amount=300, time=timedelta(days=30))
  92. def test_no_ref(self):
  93. self.without_ref.vpnuser.on_payment_confirmed(self.payment)
  94. def test_ref(self):
  95. self.with_ref.vpnuser.on_payment_confirmed(self.payment)
  96. self.assertTrue(self.with_ref.vpnuser.referrer_used)
  97. self.assertEqual(self.with_ref.vpnuser.referrer, self.referrer)
  98. self.assertRemaining(self.referrer.vpnuser, timedelta(days=14))
  99. class GCModelTest(TestCase):
  100. def test_generator(self):
  101. c = random_gift_code()
  102. self.assertEqual(len(c), 10)
  103. self.assertNotEqual(c, random_gift_code())
  104. class SignupViewTest(TestCase):
  105. def test_form(self):
  106. response = self.client.get('/account/signup')
  107. self.assertEqual(response.status_code, 200)
  108. self.assertIsInstance(response.context['form'], SignupForm)
  109. def test_post(self):
  110. response = self.client.post('/account/signup', {
  111. 'username': 'test_un', 'password': 'test_pw', 'password2': 'test_pw'})
  112. self.assertRedirects(response, '/account/')
  113. user = User.objects.get(username='test_un')
  114. self.assertTrue(user.check_password('test_pw'))
  115. def test_post_error(self):
  116. response = self.client.post('/account/signup', {
  117. 'username': 'test_un', 'password': 'test_pw', 'password2': 'qsdf'})
  118. self.assertEqual(response.status_code, 200)
  119. self.assertIsInstance(response.context['form'], SignupForm)
  120. self.assertFormError(response, 'form', 'password',
  121. 'Passwords are not the same')
  122. def test_post_referrer(self):
  123. ref = User.objects.create_user('referrer')
  124. response = self.client.post('/account/signup?ref=%d' % ref.id, {
  125. 'username': 'test_un', 'password': 'test_pw', 'password2': 'test_pw'})
  126. self.assertRedirects(response, '/account/')
  127. user = User.objects.get(username='test_un')
  128. self.assertTrue(user.check_password('test_pw'))
  129. self.assertEqual(user.vpnuser.referrer, ref)
  130. class AccountViewsTest(TestCase, UserTestMixin):
  131. def setUp(self):
  132. User.objects.create_user('test', None, 'testpw')
  133. self.client.login(username='test', password='testpw')
  134. def test_account(self):
  135. response = self.client.get('/account/')
  136. self.assertEqual(response.status_code, 200)
  137. def test_trial_get(self):
  138. response = self.client.get('/account/trial')
  139. self.assertRedirects(response, '/account/')
  140. def test_trial(self):
  141. p = timedelta(days=1)
  142. with self.settings(RECAPTCHA_API='TEST'):
  143. with override_config(TRIAL_PERIOD_HOURS=24, TRIAL_PERIOD_MAX=2):
  144. good_data = {'g-recaptcha-response': 'TEST-TOKEN'}
  145. response = self.client.post('/account/trial', good_data)
  146. self.assertRedirects(response, '/account/')
  147. user = User.objects.get(username='test')
  148. self.assertRemaining(user.vpnuser, p)
  149. def test_trial_fail(self):
  150. p = timedelta(days=1)
  151. with self.settings(RECAPTCHA_API='TEST'):
  152. with override_config(TRIAL_PERIOD_HOURS=24, TRIAL_PERIOD_MAX=2):
  153. bad_data = {'g-recaptcha-response': 'TOTALLY-NOT-TEST-TOKEN'}
  154. response = self.client.post('/account/trial', bad_data)
  155. self.assertRedirects(response, '/account/')
  156. user = User.objects.get(username='test')
  157. self.assertRemaining(user.vpnuser, timedelta())
  158. def test_settings_form(self):
  159. response = self.client.get('/account/settings')
  160. self.assertEqual(response.status_code, 200)
  161. def test_settings_post(self):
  162. response = self.client.post('/account/settings', {
  163. 'password': 'new_test_pw', 'password2': 'new_test_pw',
  164. 'email': 'new_email@example.com'})
  165. self.assertEqual(response.status_code, 200)
  166. user = User.objects.get(username='test')
  167. self.assertTrue(user.check_password('new_test_pw'))
  168. self.assertEqual(user.email, 'new_email@example.com')
  169. def test_settings_post_fail(self):
  170. response = self.client.post('/account/settings', {
  171. 'password': 'new_test_pw', 'password2': 'new_test_pw_qsdfg',
  172. 'email': 'new_email@example.com'})
  173. self.assertEqual(response.status_code, 200)
  174. user = User.objects.get(username='test')
  175. self.assertFalse(user.check_password('new_test_pw'))
  176. self.assertEqual(user.email, 'new_email@example.com')
  177. def test_giftcode_use_single(self):
  178. gc = GiftCode.objects.create(time=timedelta(days=42), single_use=True)
  179. response = self.client.post('/account/gift_code', {'code': gc.code})
  180. self.assertRedirects(response, '/account/')
  181. user = User.objects.get(username='test')
  182. self.assertRemaining(user.vpnuser, timedelta(days=42))
  183. response = self.client.post('/account/gift_code', {'code': gc.code})
  184. self.assertRedirects(response, '/account/')
  185. user = User.objects.get(username='test')
  186. self.assertRemaining(user.vpnuser, timedelta(days=42)) # same expiration
  187. def test_giftcode_use_free_only(self):
  188. gc = GiftCode.objects.create(time=timedelta(days=42), free_only=True)
  189. response = self.client.post('/account/gift_code', {'code': gc.code})
  190. self.assertRedirects(response, '/account/')
  191. user = User.objects.get(username='test')
  192. self.assertRemaining(user.vpnuser, timedelta(days=42))
  193. def test_giftcode_use_free_only_fail(self):
  194. gc = GiftCode.objects.create(time=timedelta(days=42), free_only=True)
  195. user = User.objects.get(username='test')
  196. user.vpnuser.add_paid_time(timedelta(days=1))
  197. user.vpnuser.save()
  198. response = self.client.post('/account/gift_code', {'code': gc.code})
  199. self.assertRedirects(response, '/account/')
  200. user = User.objects.get(username='test')
  201. self.assertRemaining(user.vpnuser, timedelta(days=1))
  202. def test_giftcode_create_gcu(self):
  203. gc = GiftCode.objects.create(time=timedelta(days=42))
  204. response = self.client.post('/account/gift_code', {'code': gc.code})
  205. self.assertRedirects(response, '/account/')
  206. user = User.objects.get(username='test')
  207. gcu = GiftCodeUser.objects.get(user=user, code=gc)
  208. self.assertRemaining(user.vpnuser, timedelta(days=42))
  209. self.assertIn(gcu, user.giftcodeuser_set.all())
  210. class CACrtViewTest(TestCase):
  211. def test_ca_crt(self):
  212. with self.settings(OPENVPN_CA='test ca'):
  213. response = self.client.get('/ca.crt')
  214. self.assertEqual(response.status_code, 200)
  215. self.assertEqual(response['Content-Type'], 'application/x-x509-ca-cert')
  216. self.assertEqual(response.content, b'test ca')
  217. def email_text(body):
  218. return body.replace('\n', ' ') \
  219. .replace('\xa0', ' ') # nbsp
  220. class ExpireNotifyTest(TestCase):
  221. def setUp(self):
  222. pass
  223. def test_notify_first(self):
  224. out = StringIO()
  225. u = User.objects.create_user('test_username', 'test@example.com', 'testpw')
  226. u.vpnuser.add_paid_time(timedelta(days=2))
  227. u.vpnuser.save()
  228. call_command('expire_notify', stdout=out)
  229. self.assertEqual(len(mail.outbox), 1)
  230. self.assertEqual(mail.outbox[0].to, ['test@example.com'])
  231. self.assertIn('expire in 1 day', email_text(mail.outbox[0].body))
  232. u = User.objects.get(username='test_username')
  233. self.assertAlmostEqual(u.vpnuser.last_expiry_notice, timezone.now(),
  234. delta=timedelta(minutes=1))
  235. def test_notify_second(self):
  236. out = StringIO()
  237. u = User.objects.create_user('test_username', 'test@example.com', 'testpw')
  238. u.vpnuser.last_expiry_notice = timezone.now() - timedelta(days=2)
  239. u.vpnuser.add_paid_time(timedelta(days=1))
  240. u.vpnuser.save()
  241. call_command('expire_notify', stdout=out)
  242. self.assertEqual(len(mail.outbox), 1)
  243. self.assertEqual(mail.outbox[0].to, ['test@example.com'])
  244. self.assertIn('expire in 23 hours, 59 minutes', email_text(mail.outbox[0].body))
  245. u = User.objects.get(username='test_username')
  246. self.assertAlmostEqual(u.vpnuser.last_expiry_notice, timezone.now(),
  247. delta=timedelta(minutes=1))
  248. def test_notify_subscription(self):
  249. out = StringIO()
  250. u = User.objects.create_user('test_username', 'test@example.com', 'testpw')
  251. u.vpnuser.add_paid_time(timedelta(days=2))
  252. u.vpnuser.save()
  253. s = Subscription(user=u, backend_id='paypal', status='active')
  254. s.save()
  255. call_command('expire_notify', stdout=out)
  256. self.assertEqual(len(mail.outbox), 0)
  257. u = User.objects.get(username='test_username')
  258. # FIXME:
  259. # self.assertNotAlmostEqual(u.vpnuser.last_expiry_notice, timezone.now(),
  260. # delta=timedelta(minutes=1))
  261. def test_notify_subscription_new(self):
  262. out = StringIO()
  263. u = User.objects.create_user('test_username', 'test@example.com', 'testpw')
  264. u.vpnuser.add_paid_time(timedelta(days=2))
  265. u.vpnuser.last_expiry_notice = timezone.now() - timedelta(days=5)
  266. u.vpnuser.save()
  267. s = Subscription(user=u, backend_id='paypal', status='new')
  268. s.save()
  269. call_command('expire_notify', stdout=out)
  270. self.assertEqual(len(mail.outbox), 1)
  271. u = User.objects.get(username='test_username')
  272. # FIXME:
  273. # self.assertNotAlmostEqual(u.vpnuser.last_expiry_notice, timezone.now(),
  274. # delta=timedelta(minutes=1))