def test_get_not_message_info(self, get_message_info): factories.SmsMessageFactory(eid=None) qry = SmsMessage.objects.all() request = RequestFactory() request._messages = BaseStorage(request) SmsMessageAdmin(SmsMessage, None).get_message_info(request, qry) self.assertFalse(get_message_info.called)
def test_simple_form(monkeypatch, rf, admin_user, greeting_form, push_messages, valid_data, method, anon, override_form): fd = greeting_form message = f'zzz-å{get_random_string(12)}Ö' data = { 'greeting': message, 'upload': ContentFile(VERY_SMALL_JPEG, name='hello.jpg'), fd.submit_flag_name: 'true', } if not valid_data: data.pop('greeting') if method == 'POST': request = rf.post('/', data) elif method == 'GET': data.pop('upload') # can't upload via GET request = rf.get('/', data) request.user = (AnonymousUser() if anon else admin_user) request._messages = BaseStorage(request) kwargs = dict( push_messages=push_messages, disable_redirection=True, ) if override_form == 'kwarg': kwargs['form_class'] = OverriddenDesignedForm elif override_form == 'settings': # Can't use the pytest-django settings fixture, since `form_designer.settings` # has non-lazy copies of Django settings taken at that module's import time. monkeypatch.setattr( fd_settings, 'DESIGNED_FORM_CLASS', 'form_designer.tests.test_basics.OverriddenDesignedForm') context = process_form(request, fd, **kwargs) assert context['form_success'] == valid_data # Test that a message was (or was not) pushed assert len(request._messages._queued_messages) == int(push_messages) if valid_data: if override_form: # If we've overridden the form, we expect an uppercase message message = message.upper() # Test that the form log was saved: flog = FormLog.objects.get(form_definition=fd) assert flog == context[ 'form_log'] # (and it's the same object in the context) name_to_value = {d['name']: d['value'] for d in flog.data} assert name_to_value['greeting'] == message if name_to_value.get('upload'): assert isinstance(name_to_value['upload'], File) if not anon: assert flog.created_by == admin_user # Test that the email was sent: sent_email = mail.outbox[-1] assert message in sent_email.subject # (since we customized the subject with a template) assert 'greetingbot' in sent_email.message().get( "Reply-To") # (since we customized the reply-to address)
def init_request(): request = HttpRequest() request.method = 'GET' request.session = SessionStore() request.session.create() request._messages = BaseStorage(request) request.entree_user = AnonymousUser() return request
def test_social_account_adapter_signup(self): """Test that our Allauth social account adapter correctly handles signups.""" adapter = SocialAccountAdapter() discord_login = SocialLogin(account=self.discord_account) discord_login_role = SocialLogin(account=self.discord_account_one_role) discord_login_not_present = SocialLogin( account=self.discord_account_not_present) discord_login_two_roles = SocialLogin( account=self.discord_account_two_roles) github_login = SocialLogin(account=self.github_account) messages_request = self.request_factory.get("/") messages_request._messages = BaseStorage(messages_request) with patch("pydis_site.utils.account.reverse") as mock_reverse: with patch("pydis_site.utils.account.redirect") as mock_redirect: with self.assertRaises(ImmediateHttpResponse): adapter.is_open_for_signup(messages_request, github_login) with self.assertRaises(ImmediateHttpResponse): adapter.is_open_for_signup(messages_request, discord_login) with self.assertRaises(ImmediateHttpResponse): adapter.is_open_for_signup(messages_request, discord_login_role) with self.assertRaises(ImmediateHttpResponse): adapter.is_open_for_signup(messages_request, discord_login_not_present) self.assertTrue( adapter.is_open_for_signup(messages_request, discord_login_two_roles)) self.assertEqual( len(messages_request._messages._queued_messages), 4) self.assertEqual(mock_redirect.call_count, 4) self.assertEqual(mock_reverse.call_count, 4)