How to use set_random_key_for_user method in Kiwi

Best Python code snippet using Kiwi_python

tests.py

Source:tests.py Github

copy

Full Screen

...23 mock_datetime.datetime.today.return_value = datetime.datetime(2017, 5, 10)24 mock_datetime.timedelta.return_value = datetime.timedelta(7)25 fake_random = 0.1234567826 random.random.return_value = fake_random27 activation_key = UserActivateKey.set_random_key_for_user(self.new_user)28 self.assertEqual(self.new_user, activation_key.user)29 s_random = sha1(str(fake_random).encode('utf-8')).hexdigest()[:5]30 expected_key = sha1('{}{}'.format(31 s_random, self.new_user.username).encode('utf-8')).hexdigest()32 self.assertEqual(expected_key, activation_key.activation_key)33 self.assertEqual(datetime.datetime(2017, 5, 17),34 activation_key.key_expires)35class TestForceToSetRandomKey(TestCase):36 """Test case for UserActivateKey.set_random_key_for_user forcely"""37 @classmethod38 def setUpTestData(cls):39 cls.new_user = User.objects.create(username='new-tester',40 email='new-tester@example.com',41 password='password')42 cls.origin_activation_key = UserActivateKey.set_random_key_for_user(cls.new_user)43 def test_set_random_key_forcely(self):44 new_activation_key = UserActivateKey.set_random_key_for_user(self.new_user,45 force=True)46 self.assertEqual(self.origin_activation_key.user, new_activation_key.user)47 self.assertNotEqual(self.origin_activation_key.activation_key,48 new_activation_key.activation_key)49# ### Test cases for view methods ###50class TestLogout(TestCase):51 """Test for logout view method"""52 @classmethod53 def setUpTestData(cls):54 super(TestLogout, cls).setUpTestData()55 cls.tester = User.objects.create_user(username='authtester',56 email='authtester@example.com',57 password='password')58 cls.logout_url = reverse('tcms-logout')59 def test_logout_then_redirect_to_next(self):60 self.client.login(username=self.tester.username, password='password')61 response = self.client.get(self.logout_url, follow=True)62 self.assertRedirects(response, reverse('tcms-login'))63 def test_logout_then_goto_next(self):64 self.client.login(username=self.tester.username, password='password')65 next_url = reverse('plans-all')66 response = self.client.get(self.logout_url, {'next': next_url}, follow=True)67 self.assertRedirects(response, next_url)68class TestRegistration(TestCase):69 def setUp(self):70 self.register_url = reverse('tcms-register')71 self.fake_activate_key = 'secret-activate-key'72 def test_open_registration_page(self):73 response = self.client.get(self.register_url)74 self.assertContains(75 response,76 '<input value="Register" class="loginbutton sprites" type="submit">',77 html=True)78 @patch('tcms.core.contrib.auth.models.sha1')79 def assert_user_registration(self, username, sha1, follow=False):80 sha1.return_value.hexdigest.return_value = self.fake_activate_key81 response = self.client.post(self.register_url,82 {'username': username,83 'password1': 'password',84 'password2': 'password',85 'email': 'new-tester@example.com'},86 follow=follow)87 users = User.objects.filter(username=username)88 self.assertTrue(users.exists())89 user = users[0]90 self.assertEqual('new-tester@example.com', user.email)91 self.assertFalse(user.is_active)92 keys = UserActivateKey.objects.filter(user=user)93 self.assertTrue(keys.exists())94 self.assertEqual(self.fake_activate_key, keys[0].activation_key)95 return response96 @patch('tcms.signals.USER_REGISTERED_SIGNAL.send')97 def test_register_user_sends_signal(self, signal_mock):98 self.assert_user_registration('new-signal-tester')99 self.assertTrue(signal_mock.called)100 self.assertEqual(1, signal_mock.call_count)101 @override_settings(ADMINS=[('Test Admin', 'admin@kiwitcms.org')])102 @patch('tcms.core.utils.mailto.send_mail')103 def test_signal_handler_notifies_admins(self, send_mail):104 # connect the handler b/c it is not connected by default105 signals.USER_REGISTERED_SIGNAL.connect(signals.notify_admins)106 try:107 response = self.assert_user_registration('signal-handler')108 self.assertRedirects(response, reverse('core-views-index'), target_status_code=302)109 # 1 - verification mail, 2 - email to admin110 self.assertTrue(send_mail.called)111 self.assertEqual(2, send_mail.call_count)112 # verify we've actually sent the admin email113 self.assertIn('New user awaiting approval', send_mail.call_args_list[0][0][0])114 self.assertIn('somebody just registered an account with username signal-handler',115 send_mail.call_args_list[0][0][1])116 self.assertIn('admin@kiwitcms.org', send_mail.call_args_list[0][0][-1])117 finally:118 signals.USER_REGISTERED_SIGNAL.disconnect(signals.notify_admins)119 @patch('tcms.core.utils.mailto.send_mail')120 def test_register_user_by_email_confirmation(self, send_mail):121 response = self.assert_user_registration('new-tester', follow=True)122 self.assertContains(123 response,124 'Your account has been created, please check your mailbox for confirmation'125 )126 s = Site.objects.get_current()127 confirm_url = 'http://%s%s' % (s.domain, reverse('tcms-confirm',128 args=[self.fake_activate_key]))129 # Verify notification mail130 send_mail.assert_called_once_with(131 settings.EMAIL_SUBJECT_PREFIX + 'Your new 127.0.0.1:8000 account confirmation',132 """Welcome, new-tester, and thanks for signing up for an 127.0.0.1:8000 account!133%s134""" % confirm_url,135 settings.DEFAULT_FROM_EMAIL, ['new-tester@example.com'], fail_silently=False)136 @override_settings(AUTO_APPROVE_NEW_USERS=False,137 ADMINS=[('admin1', 'admin1@example.com'),138 ('admin2', 'admin2@example.com')])139 def test_register_user_and_activate_by_admin(self):140 response = self.assert_user_registration('plan-tester', follow=True)141 self.assertContains(142 response,143 'Your account has been created, but you need an administrator to activate it')144 for (name, email) in settings.ADMINS:145 self.assertContains(response,146 '<a href="mailto:{}">{}</a>'.format(email, name),147 html=True)148class TestConfirm(TestCase):149 """Test for activation key confirmation"""150 @classmethod151 def setUpTestData(cls):152 cls.new_user = User.objects.create(username='new-user',153 email='new-user@example.com',154 password='password')155 def setUp(self):156 self.new_user.is_active = False157 self.new_user.save()158 def test_fail_if_activation_key_does_not_exist(self):159 confirm_url = reverse('tcms-confirm',160 args=['nonexisting-activation-key'])161 response = self.client.get(confirm_url, follow=True)162 self.assertContains(163 response,164 'This activation key no longer exists in the database')165 # user account not activated166 user = User.objects.get(username=self.new_user.username)167 self.assertFalse(user.is_active)168 def test_fail_if_activation_key_expired(self):169 fake_activation_key = 'secret-activation-key'170 with patch('tcms.core.contrib.auth.models.sha1') as sha1:171 sha1.return_value.hexdigest.return_value = fake_activation_key172 key = UserActivateKey.set_random_key_for_user(self.new_user)173 key.key_expires = datetime.datetime.now() - datetime.timedelta(days=10)174 key.save()175 confirm_url = reverse('tcms-confirm', args=[fake_activation_key])176 response = self.client.get(confirm_url, follow=True)177 self.assertContains(response, 'This activation key has expired')178 # user account not activated179 user = User.objects.get(username=self.new_user.username)180 self.assertFalse(user.is_active)181 def test_confirm(self):182 fake_activate_key = 'secret-activate-key'183 with patch('tcms.core.contrib.auth.models.sha1') as sha1:184 sha1.return_value.hexdigest.return_value = fake_activate_key185 UserActivateKey.set_random_key_for_user(self.new_user)186 confirm_url = reverse('tcms-confirm',187 args=[fake_activate_key])188 response = self.client.get(confirm_url, follow=True)189 self.assertContains(190 response,191 'Your account has been activated successfully')192 # user account activated193 user = User.objects.get(username=self.new_user.username)194 self.assertTrue(user.is_active)195 activate_key_deleted = not UserActivateKey.objects.filter(user=user).exists()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kiwi automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful