def test_non_staff_user(self): self.client.logout() user = UserFactory(email='*****@*****.**') user.set_password('secret') self.client.login(username=user.email, password='******') response = self.client.get(reverse('source_code_upload')) self.assertTrue('Log in' in response.content)
class NewUserTests(SeleniumTestCase): """Tests Account creation""" def setUp(self): self.user = UserFactory(first_name="John", last_name="Doe") def test_home_page_works(self): """ As John, navigating to https://secure.my.jobs should send me to a page titled "My.jobs". """ self.browser.get(self.live_server_url) self.assertIn(self.browser.title, 'My.jobs') def test_cant_log_in_without_account(self): """ As John, I shouldn't be able to log into My.jobs without registering first. """ self.browser.get('/'.join([self.live_server_url, 'prm', 'view'])) # attempt to log in username = self.find('id_username') username.send_keys(self.user.email) self.find('id_password').send_keys(self.user.password) self.find('login').click() self.assertEqual(username.get_attribute('placeholder'), 'Please enter a correct email.') def test_user_registration(self): """ As John, I should be able to register on My.jobs and log in. """ user = UserFactory.build(email='*****@*****.**') self.browser.get('/'.join([self.live_server_url, 'prm', 'view'])) # register self.find('id_email').send_keys(user.email) self.find('id_password1').send_keys(user.password) self.find('id_password2').send_keys(user.password) self.find('register').click() self.assertEqual(self.find('profile').get_attribute( 'innerHTML'), 'Skip: Take me to my profile') def test_user_login(self): self.user.set_password("test") self.user.save() self.find('id_username').send_keys(self.user.email) self.find('id_password').send_keys("test") self.find('login').click()
class MyReportsTestCase(TestCase): """ Base class for all MyReports Tests. Identical to `django.test.TestCase` except that it provides a MyJobs TestClient instance and a logged in user. """ def setUp(self): self.client = TestClient() self.user = UserFactory(email='*****@*****.**') self.user.set_password('aa') self.company = CompanyFactory(name='Test Company') self.partner = PartnerFactory(name='Test Partner', owner=self.company) # associate company to user CompanyUserFactory(user=self.user, company=self.company) self.client.login_user(self.user) create_full_fixture()
class NewUserTests(SeleniumTestCase): """Tests Account creation""" def setUp(self): super(NewUserTests, self).setUp() company = CompanyFactory() self.user = UserFactory(first_name="John", last_name="Doe") admin_role = RoleFactory(company=company, name='Admin') self.user.roles.add(self.admin_role) def test_home_page_works(self): """ As John, navigating to https://secure.my.jobs should send me to a page titled "My.jobs". """ self.browser.get(self.live_server_url) self.assertIn(self.browser.title, 'My.jobs') def test_cant_log_in_without_account(self): """ As John, I shouldn't be able to log into My.jobs without registering first. """ self.browser.get('/'.join([self.live_server_url, 'prm', 'view'])) # We're trying to access a private page while unauthenticated, which # should result in a next parameter being added. self.assertTrue('next=' in self.browser.current_url) # attempt to log in username = self.find('id_username') username.send_keys(self.user.email) self.find('id_password').send_keys(self.user.password) self.find('login').click() # If we've logged in, the next parameter should have went away. We # aren't expecting to be logged in right now as the password was bad. self.assertTrue('next=' in self.browser.current_url) def test_user_registration(self): """ As John, I should be able to register on My.jobs and log in. """ self.browser.get('/'.join([self.live_server_url, 'prm', 'view'])) # register self.find('id_email').send_keys('*****@*****.**') self.find('id_password1').send_keys('aaAA11..') self.find('id_password2').send_keys('aaAA11..') self.find('register').click() try: WebDriverWait(self.browser, 10).until( expected_conditions.presence_of_element_located( (By.ID, 'profile'))) finally: self.assertEqual(self.find('profile').get_attribute( 'innerHTML'), 'Skip: Take me to my profile') def test_user_login(self): self.user.set_password("test") self.user.save() self.find('id_username').send_keys(self.user.email) self.find('id_password').send_keys("test") self.find('login').click()
class NewUserTests(SeleniumTestCase): """Tests Account creation""" def setUp(self): super(NewUserTests, self).setUp() company = CompanyFactory() self.user = UserFactory(first_name="John", last_name="Doe") admin_role = RoleFactory(company=company, name='Admin') self.user.roles.add(self.admin_role) def test_home_page_works(self): """ As John, navigating to https://secure.my.jobs should send me to a page titled "My.jobs". """ self.browser.get(self.live_server_url) self.assertIn(self.browser.title, 'My.jobs') def test_cant_log_in_without_account(self): """ As John, I shouldn't be able to log into My.jobs without registering first. """ self.browser.get('/'.join([self.live_server_url, 'prm', 'view'])) # We're trying to access a private page while unauthenticated, which # should result in a next parameter being added. self.assertTrue('next=' in self.browser.current_url) # attempt to log in username = self.find('id_username') username.send_keys(self.user.email) self.find('id_password').send_keys(self.user.password) self.find('login').click() # If we've logged in, the next parameter should have went away. We # aren't expecting to be logged in right now as the password was bad. self.assertTrue('next=' in self.browser.current_url) def test_user_registration(self): """ As John, I should be able to register on My.jobs and log in. """ self.browser.get('/'.join([self.live_server_url, 'prm', 'view'])) # register self.find('id_email').send_keys('*****@*****.**') self.find('id_password1').send_keys('aaAA11..') self.find('id_password2').send_keys('aaAA11..') self.find('register').click() try: WebDriverWait(self.browser, 10).until( expected_conditions.presence_of_element_located( (By.ID, 'profile'))) finally: self.assertEqual( self.find('profile').get_attribute('innerHTML'), 'Skip: Take me to my profile') def test_user_login(self): self.user.set_password("test") self.user.save() self.find('id_username').send_keys(self.user.email) self.find('id_password').send_keys("test") self.find('login').click()
class EmailForwardTests(RedirectBase): def setUp(self): super(EmailForwardTests, self).setUp() self.redirect_guid = JOB['guid'] self.redirect = RedirectFactory(buid=JOB['buid'], guid='{%s}' % uuid.UUID(self.redirect_guid)) self.password = '******' self.user = UserFactory(email='*****@*****.**') self.user.set_password(self.password) self.user.save() self.contact = CompanyEmail.objects.create( buid=self.redirect.buid, email=self.user.email) self.email = self.user.email.replace('@', '%40') self.auth = { 'bad': [ '', 'Basic %s' % base64.b64encode('bad%40email:wrong_pass')], 'good': 'Basic %s' % base64.b64encode('%s:%s' % (self.user.email.\ replace('@', '%40'), self.password))} self.post_dict = {'to': '*****@*****.**', 'from': '*****@*****.**', 'text': 'Questions about stuff', 'html': '<b>Questions about stuff</b>', 'subject': 'Bad Email', 'attachments': 0} self.r = Replacer() self.r.replace('pysolr.Solr.search', mock_search) def tearDown(self): super(EmailForwardTests, self).tearDown() self.r.restore() def submit_email(self, use_data=True): """ Helper method for submitting parsed emails. Ensures that the request returns a status of 200. Inputs: :use_data: Should we include post data; Default: True Outputs: :response: HttpResponse from email redirect view """ auth = self.auth.get('good') kwargs = {'HTTP_AUTHORIZATION': auth} if use_data: kwargs['data'] = self.post_dict response = self.client.post(reverse('email_redirect'), **kwargs) self.assertEqual(response.status_code, 200) return response def assert_guid_email_responses_are_correct(self, redirect, job=None): """ Helper method for validating parsed [email protected] emails. Inputs: :redirect: Redirect instance to use if a job is old :job: Solr result for a new job """ email = mail.outbox.pop(0) self.assertEqual(email.from_email, settings.DEFAULT_FROM_EMAIL) self.assertEqual(email.to, [self.post_dict['from']]) self.assertEqual(email.subject, self.post_dict['subject']) # Emails turn lots of characters into HTML entities. Results from # Solr and the database do not. Unescape the email body so we can # compare the two. parser = HTMLParser.HTMLParser() body = parser.unescape(email.body) if job is not None: self.assertTrue(markdown.markdown(job['description']) in body) else: self.assertTrue(redirect.job_title in body) def test_jira_login(self): jira = JIRA(options=settings.options, basic_auth=settings.my_agent_auth) self.assertIsNotNone(jira) def test_bad_authorization(self): for auth in self.auth.get('bad'): kwargs = {} if auth: # auth_value has a value, so we can pass an HTTP_AUTHORIZATION # header kwargs['HTTP_AUTHORIZATION'] = auth response = self.client.post(reverse('email_redirect'), **kwargs) self.assertTrue(response.status_code, 403) def test_good_authorization(self): self.submit_email(use_data=False) def test_bad_email(self): self.submit_email() self.assertEqual(len(mail.outbox), 0) def test_bad_guid_email(self): self.post_dict['to'] = '*****@*****.**' % ('1'*32) self.post_dict['text'] = 'This address is not in the database' self.submit_email() email = mail.outbox.pop() self.assertEqual(email.subject, 'Email forward failure') self.assertTrue('There is no job associated with this address' in email.body) def test_good_guid_email_new_job(self): self.post_dict['to'] = ['*****@*****.**' % self.redirect_guid] self.post_dict['subject'] = 'Email forward success' self.submit_email() self.assert_guid_email_responses_are_correct(self.redirect, JOB) email = mail.outbox.pop() # email.alternatives is a list of sets as follows: # [('html body', 'text/html')] alternatives = dict((part[1], part[0]) for part in email.alternatives) self.assertTrue(JOB['html_description'] in alternatives['text/html']) self.assertTrue(email.subject.startswith('[ReqID: %s]' % JOB['reqid'])) def test_good_guid_email_new_job_no_user(self): self.contact.delete() self.post_dict['to'] = ['*****@*****.**' % self.redirect_guid] self.post_dict['subject'] = 'Email forward success' self.submit_email() self.assert_guid_email_responses_are_correct(self.redirect, JOB) def test_good_guid_email_old_job(self): guid = '1'*32 redirect = RedirectFactory(guid='{%s}' % uuid.UUID(guid), buid=self.redirect.buid, uid=1) self.post_dict['to'] = ['*****@*****.**' % guid] self.post_dict['subject'] = 'Email forward success' self.submit_email() self.assert_guid_email_responses_are_correct(redirect) email = mail.outbox.pop() self.assertTrue('This job (%s) has expired.' % ( redirect.job_title, ) in email.body) self.assertTrue(email.subject.startswith('[ReqID: Expired]')) def test_good_guid_email_old_job_no_user(self): self.contact.delete() guid = '1'*32 redirect = RedirectFactory(guid='{%s}' % uuid.UUID(guid), buid=self.redirect.buid, uid=1) self.post_dict['to'] = ['*****@*****.**' % guid] self.post_dict['subject'] = 'Email forward success' self.submit_email() self.assert_guid_email_responses_are_correct(redirect) def test_email_with_name(self): self.post_dict['to'] = 'User <*****@*****.**>' % self.redirect_guid self.post_dict['subject'] = 'Email forward success' self.submit_email() email = mail.outbox.pop() def test_no_emails(self): self.post_dict.pop('to') self.submit_email() self.assertEqual(len(mail.outbox), 0) def test_too_many_emails(self): self.post_dict['to'] = '[email protected], [email protected]' self.submit_email() self.assertEqual(len(mail.outbox), 0) def test_prm_email(self): """ If [email protected] is included as a recipient, we repost this email to My.jobs. This is a straight post, which we don't want to do in a testing environment. If we receive a 200 status code and no emails were sent, this was reasonably likely to have completed successfully. """ prm_list = ['*****@*****.**', '*****@*****.**'] for email in prm_list: # SendGrid adds [email protected] to the 'envelope' JSON string # if it appears as a BCC self.post_dict['envelope'] = '{"to":["%s"]}' % email response = self.submit_email() self.assertEqual(response.content, 'reposted') self.assertEqual(len(mail.outbox), 0) del self.post_dict['envelope'] for email in prm_list: self.post_dict['to'] = email response = self.submit_email() self.assertEqual(response.content, 'reposted') self.assertEqual(len(mail.outbox), 0)