def test_reverse_host(self): self.assertRaises(ValueError, reverse_host, 'with_kwargs', ['spam'], dict(eggs='spam')) self.assertRaises(NoReverseMatch, reverse_host, 'with_kwargs', ['spam', 'eggs']) self.assertRaises(NoReverseMatch, reverse_host, 'with_kwargs', [], dict(eggs='spam', spam='eggs')) self.assertEqual('johndoe', reverse_host('with_kwargs', None, dict(username='******'))) self.assertEqual(reverse_host('with_args', ['johndoe']), 'johndoe') with self.settings(PARENT_HOST='spam.eggs'): self.assertEqual(reverse_host('with_args', ['johndoe']), 'johndoe.spam.eggs')
def test_logged_in(self): self.client.login(username='******', password='******') response = self.client.get(self.path, HTTP_HOST=reverse_host('challenges')) self.assertEqual(str(response.context['user']), 'testuser') self.assertEqual(response.status_code, 200) self.assertEqual(response.context['data_url'], reverse_lazy('api:challenge:current'))
def dispatch(self, *args, **kwargs): """If the user is unauthenticated, redirect them to the login view.""" if self.request.user.is_anonymous(): messages.warning(self.request, 'You will need to log in first.') return redirect( reverse('login', host='foiamachine') + '?next=//' + reverse_host('foiamachine') + self.request.get_full_path()) return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)
def test_logged_in(self): self.client.login(username='******', password='******') response = self.client.get(self.path, HTTP_HOST=reverse_host('challenges')) self.assertEqual(str(response.context['user']), 'testuser') self.assertEqual(response.status_code, 200) self.assertEqual(response.context['data_url'], reverse_lazy('api:activities:list', kwargs={'user_id': self.test_user.id}))
def get_context(self, request, name): ctx = SimpleNamespace() ctx.default_host = reverse_host(settings.DEFAULT_HOST) ctx.drug = Drug.objects.get_from_name_or_404(name) ctx.interactions = (ctx.drug.interactions.prefetch_related( 'from_drug', 'to_drug').order_by('is_draft', '-risk')) return ctx
def test_users_name_with_full_name(self): self.test_user.first_name = 'Test' self.test_user.last_name = 'User' self.test_user.save() self.client.login(username='******', password='******') response = self.client.get(self.path, HTTP_HOST=reverse_host('challenges')) self.assertEqual(str(response.context['page_title']), 'Test User') self.assertEqual(response.status_code, 200)
def manager_login(request): next_page = request.GET.get('next') if request.user.is_authenticated: if next_page: return HttpResponseRedirect(next_page) return redirect('home-index') initial = {'email': request.COOKIES.get('email', '')} form = AuthenticationForm(request.POST or None, initial=initial, request=request) if form.is_valid(): data = form.cleaned_data user = None u = User.objects.get(email=data['email']) if u.check_password(data['password']): user = u if not user: form.add_error(None, _("Please enter a correct email and password.")) elif user.is_active and (user.is_staff or user.is_superuser): login(request, user) if next_page and is_safe_url(next_page, allowed_hosts=settings.ALLOWED_HOSTS): redirect_url = next_page else: redirect_url = reverse_host('home-index', host='manager') response = HttpResponseRedirect(redirect_url) response.set_cookie('email', user.email) return response elif not user.is_staff or not user.is_superuser: form.add_error(None, _("This account is not staff.")) else: form.add_error(None, _("This account is inactive.")) return render(request, 'Manager/Login/login.html', {'form': form})
def check_redirect_url( response: HttpResponseRedirect, reversed_url: str, strip_params=True ) -> bool: """ Check whether a given redirect response matches a specific reversed URL. Arguments: * `response`: The HttpResponseRedirect returned by the test client * `reversed_url`: The URL returned by `reverse()` * `strip_params`: Whether to strip URL parameters (following a "?") from the URL given in the `response` object """ host = get_host(None) hostname = reverse_host(host) redirect_url = response.url if strip_params and "?" in redirect_url: redirect_url = redirect_url.split("?", 1)[0] result = reversed_url == f"//{hostname}{redirect_url}" return result
def setUp(self): """Sets up a test client that automatically sets the correct HOST header.""" self.client = Client(HTTP_HOST=reverse_host(host="staff"))
def get_absolute_url(self): return reverse_host('sub_send_message', host='user', host_kwargs={'username': self.username}, scheme='http')
def test_post(self): self.client.post(self.path, data={'username': '******', 'password1': 'agdusgdiu39u21n', 'password2': 'agdusgdiu39u21n'}, HTTP_HOST=reverse_host('challenges')) self.assertEqual(User.objects.filter(username='******').count(), 1)
def test_get(self): response = self.client.get(self.path, HTTP_HOST=reverse_host('challenges')) self.assertEqual(response.status_code, 200)
def test_redirect_if_not_logged_in(self): response = self.client.get(self.path, HTTP_HOST=reverse_host('challenges')) self.assertEqual(response.status_code, 302)
def test_post(self): response = self.client.post(self.path, data={'username': '******', 'password': '******'}, HTTP_HOST=reverse_host('challenges')) self.assertRedirects(response, reverse_lazy('front:home'))