def setUp(self): # Uses the cookie hack from: # https://stackoverflow.com/questions/22494583/login-with-code-when-using-liveservertestcase-with-django client = Client() self.user = get_user_model().objects.create_user('a', 'b', 'c') client.login(username='******', password='******') self.cookie = client.cookies['sessionid'] self.selenium.get("{}{}".format(self.live_server_url, "/")) self.selenium.add_cookie({ 'name': 'sessionid', 'value': self.cookie.value, 'secure': False, 'path': '/' }) self.selenium.refresh() self.short = "asdf" self.project = Project(creator=self.user, name="long_asdf", name_short=self.short) self.project.save() self.project.developer.set((self.user.pk, )) self.project.manager.set((self.user.pk, )) self.project.save() si = SlackIntegration() si.project = self.project si.api_token = "foo" si.channel = "channel" si.save() self.title_name = 'This is title' self.comment = "This is comment"
def get_redirect_url(self, *args, **kwargs): if not SLACK_ID: return reverse("project:edit", kwargs={'project': self.kwargs['project']}) slack = SlackClient("") code = self.request.GET['code'] resp = slack.api_call( "oauth.access", code=code, client_id=SLACK_ID, client_secret=SLACK_SECRET, redirect_uri="https://" + HOST + reverse("integration:slack:auth", kwargs={'project': self.kwargs['project']}), ) if resp['ok']: si = SlackIntegration() si.api_token = resp['access_token'] si.project = Project.objects.get(name_short=self.kwargs['project']) si.save() return reverse("integration:slack:update", kwargs={ 'project': self.kwargs['project'], 'pk': si.pk }) return reverse("project:edit", kwargs={'project': self.kwargs['project']})
class ConnectTest(CallbackTestBase): @classmethod def setUpTestData(self): # NOTE: if you modify these elements they need to be created in setUp(), instead of here super().setUpTestData() # Send an early signal to avoid event.signals.connector being called # later. signals.modify.send(sender=Issue) def callback(*args, **kwargs): self.called = (self.called[0] + 1, args, kwargs) self.si = SlackIntegration(api_token="token", channel="channel") self.si.on_issue_signal = callback self.si.on_sprint_signal = callback self.si.project = self.project self.si.save() # this also connects the signals def setUp(self): self.clean_callback() def test_connect_signals(self): for signal in [signals.create, signals.modify]: signal.send(sender=Issue, somekey="someval") self.assertCalled(signal=signal, somekey="someval") for signal in [signals.start, signals.stop]: signal.send(sender=Sprint, somekey="someval") self.assertCalled(signal=signal, somekey="someval") def test_disconnect_late(self): ''' Make sure that unsetting a notification actually disconnects the signal. ''' self.si.notify_issue_create = False self.si.save() signals.modify.send(sender=Issue) self.assertCalled(signal=signals.modify) signals.create.send(sender=Issue) self.assertNotCalled()
class ViewTest(TestCase): short = 'proj' @classmethod def setUpTestData(cls): # NOTE: if you modify those elements they need to be created in setUp, instead of here cls.user = get_user_model().objects.create_user('a', 'b', 'c') cls.user2 = get_user_model().objects.create_user('d', 'e', 'f') cls.project = Project(creator=cls.user, name_short=cls.short) cls.project.save() cls.project.developer.set((cls.user.pk,)) cls.project.manager.set((cls.user.pk,)) cls.project.save() def setUp(self): self.client.force_login(self.user) self.si = SlackIntegration(api_token="token", channel="channel") self.si.project = self.project self.si.save() def test_view_and_template(self): # TODO TESTCASE invite_users # use view_and_template() # TODO which views? # - integration:slack:update # - integration:slack:delete # - integration:slack:auth # - ... pass def test_redirect_to_login_and_login_required(self): self.client.force_login(self.user2) # TODO TESTCASE this uses post - include this in redirect_to_login_required() response = self.client.post(reverse('integration:slack:update', kwargs={'pk': 1, 'project': self.short}), {'channel': 'foo'}, follow=True ) self.assertContains(response, "Your account doesn't have access to this page") si = SlackIntegration.objects.get(pk=self.si.pk) self.assertEqual(si.channel, 'channel') # TODO TESTCASE this uses post - include this in redirect_to_login_required() response = self.client.post(reverse('integration:slack:delete', kwargs={'pk': 1, 'project': self.short}), {'delete': True}, follow=True ) self.assertContains(response, "Your account doesn't have access to this page") si = SlackIntegration.objects.get(pk=self.si.pk) self.assertEqual(si.channel, 'channel') # TODO TESTCASE SlackIntegrationOAuthView - integration:slack:auth self.client.force_login(self.user) def test_user_passes_test_mixin(self): # TODO TESTCASE # TODO only devs are allowed to edit an integration # TODO how about creation of integrations? pass def test_form(self): response = self.client.post( reverse('integration:slack:update', kwargs={'pk': 1, 'project': self.short}), {'channel': 'foo'} ) self.assertRedirects(response, reverse('project:edit', kwargs={'project': self.short})) si = SlackIntegration.objects.get(pk=self.si.pk) self.assertEqual(si.channel, "foo") def test_delete_slackintegration(self): response = self.client.post(reverse('integration:slack:delete', kwargs={'pk': 1, 'project': self.short}), {'delete': 'true'}) self.assertRedirects(response, reverse('project:edit', kwargs={'project': self.short})) self.assertEqual(len(SlackIntegration.objects.all()), 0) def test_keep_and_dont_delete_slackintegration(self): response = self.client.post(reverse('integration:slack:delete', kwargs={'pk': 1, 'project': self.short}), {'keep': 'true'}) self.assertRedirects(response, reverse('project:edit', kwargs={'project': self.short})) self.assertEqual(len(SlackIntegration.objects.all()), 1) @patch('integration.views.SlackClient') def test_oauth_view_not_ok(self, slackmock): slackmock().api_call.return_value = {'ok': False} response = self.client.post(reverse('integration:slack:auth', kwargs={'project': self.short}) + "?code=foo") self.assertRedirects(response, reverse('project:edit', kwargs={'project': self.short})) slackmock.assert_called_with('') slackmock().api_call.assert_called_with( "oauth.access", code="foo", client_id=SLACK_ID, client_secret=SLACK_SECRET, redirect_uri="https://" + HOST + reverse("integration:slack:auth", kwargs={'project': self.short}), ) @patch('integration.views.SlackClient') def test_oauth_view_ok(self, slackmock): slackmock().api_call.return_value = {'ok': True, 'access_token': "foo"} response = self.client.post(reverse('integration:slack:auth', kwargs={'project': self.short}) + "?code=foo") self.assertRedirects(response, reverse('integration:slack:update', kwargs={'pk': 2, 'project': self.short})) slackmock.assert_called_with('') slackmock().api_call.assert_called_with( "oauth.access", code="foo", client_id=SLACK_ID, client_secret=SLACK_SECRET, redirect_uri="https://" + HOST + reverse("integration:slack:auth", kwargs={'project': self.short}), )
class ViewTest(TestCase): short = 'proj' @classmethod def setUpTestData(cls): # NOTE: if you modify these elements they need to be created in setUp(), instead of here cls.user = get_user_model().objects.create_user('a', 'b', 'c') cls.user2 = get_user_model().objects.create_user('d', 'e', 'f') cls.project = Project(creator=cls.user, name_short=cls.short) cls.project.save() cls.project.developer.set((cls.user.pk, )) cls.project.manager.set((cls.user.pk, )) cls.project.save() def setUp(self): self.client.force_login(self.user) # NOTE: these elements get modified by some testcases, so they should NOT be created in setUpTestData() self.si = SlackIntegration(api_token="token", channel="channel") self.si.project = self.project self.si.save() def test_view_and_template(self): # TODO TESTCASE integration view and template # - integration:slack:auth # TODO does auth even need a project? # view_and_template(self, SlackIntegrationOAuthView, '', 'integration:slack:auth', # address_kwargs={'project': self.project.name_short}) # - integration:slack:update # view_and_template(self, SlackIntegrationOAuthView, '', 'integration:slack:update', # address_kwargs={'pk': 1, 'project': self.project.name_short}) # TODO needs probably more get-kwargs # - integration:slack:delete # TODO needs probably more get-kwargs # view_and_template(self, SlackIntegrationOAuthView, '', 'integration:slack:delete', # address_kwargs={'pk': 1, 'project': self.project.name_short}) pass def test_redirect_to_login_and_login_required(self): # TODO TESTCASE pass def test_user_passes_test_mixin(self): # non dev can neither update ... self.client.force_login(self.user2) user_doesnt_pass_test_and_gets_404(self, 'integration:slack:update', address_kwargs={ 'pk': 1, 'project': self.short }, get_kwargs={'channel': 'foo'}) si = SlackIntegration.objects.get(pk=self.si.pk) self.assertEqual(si.channel, 'channel') # ... nor delete the integration user_doesnt_pass_test_and_gets_404(self, 'integration:slack:delete', address_kwargs={ 'pk': 1, 'project': self.short }, get_kwargs={'delete': True}) si = SlackIntegration.objects.get(pk=self.si.pk) self.assertEqual(si.channel, 'channel') # TODO TESTCASE # TODO only devs are allowed to edit an integration # TODO how about creation of integrations? pass def test_form(self): response = self.client.post( reverse('integration:slack:update', kwargs={ 'pk': 1, 'project': self.short }), {'channel': 'foo'}) self.assertRedirects( response, reverse('project:edit', kwargs={'project': self.short})) si = SlackIntegration.objects.get(pk=self.si.pk) self.assertEqual(si.channel, "foo") def test_delete_slackintegration(self): response = self.client.post( reverse('integration:slack:delete', kwargs={ 'pk': 1, 'project': self.short }), {'delete': 'true'}) self.assertRedirects( response, reverse('project:edit', kwargs={'project': self.short})) self.assertEqual(len(SlackIntegration.objects.all()), 0) def test_keep_and_dont_delete_slackintegration(self): response = self.client.post( reverse('integration:slack:delete', kwargs={ 'pk': 1, 'project': self.short }), {'keep': 'true'}) self.assertRedirects( response, reverse('project:edit', kwargs={'project': self.short})) self.assertEqual(len(SlackIntegration.objects.all()), 1) @patch('integration.views.WebClient') def test_oauth_view_not_ok(self, slackmock): slackmock().oauth_access.return_value = {'ok': False} response = self.client.post( reverse('integration:slack:auth', kwargs={'project': self.short}) + "?code=foo") self.assertRedirects( response, reverse('project:edit', kwargs={'project': self.short})) slackmock.assert_called_with('') slackmock().oauth_access.assert_called_with( code="foo", client_id=SLACK_ID, client_secret=SLACK_SECRET, redirect_uri="https://" + HOST + reverse("integration:slack:auth", kwargs={'project': self.short}), ) @patch('integration.views.WebClient') def test_oauth_view_ok(self, slackmock): slackmock().oauth_access.return_value = { 'ok': True, 'access_token': "foo" } response = self.client.post( reverse('integration:slack:auth', kwargs={'project': self.short}) + "?code=foo") self.assertRedirects( response, reverse('integration:slack:update', kwargs={ 'pk': 2, 'project': self.short })) slackmock.assert_called_with('') slackmock().oauth_access.assert_called_with( code="foo", client_id=SLACK_ID, client_secret=SLACK_SECRET, redirect_uri="https://" + HOST + reverse("integration:slack:auth", kwargs={'project': self.short}), )