def test_calls_auth_login_if_authenticate_returns_a_user(self, mock_login, mock_authenticate): request = HttpRequest() request.POST['assertion'] = 'asserted' mock_user = mock_authenticate.return_value login(request) mock_login.assert_called_once_with(request, mock_user)
def test_does_not_call_auth_login_if_authenticate_returns_None( self, mock_authenticate, mock_auth_login ): request = HttpRequest() request.POST['assertion'] = 'asserted' mock_authenticate.return_value = None login(request) self.assertFalse(mock_auth_login.called)
def test_calls_auth_login_if_authenticate_returns_a_user( self, mock_authenticate, mock_auth_login ): request = HttpRequest() request.POST['assertion'] = 'asserted' mock_user = mock_authenticate.return_value login(request) mock_auth_login.assert_called_once_with(request, mock_user)
def test_logged_in(self): request = self.factory.get(self.url) request.user = self.user response = views.login(request) self.assertEqual(response.status_code, 302) self.assertIn("location", response._headers) self.assertIn(reverse("users:profile"), response._headers["location"][1])
def index(request): """ 首页index """ if request.method == "POST": form = register_form(data=request.POST) if form.is_valid(): form.save() email = form.cleaned_data['email'] user = MyUser.objects.get(email=email) login(request, user) return redirect(reverse('base_info_edit')) # 跳转到基本信息编辑界面 #return redirect('login') # 跳转到登录页面 if request.method == "GET": try: groups_list = Group.objects.filter( category__id=request.GET["c_id"]).order_by("-last_topic_add") except MultiValueDictKeyError: groups_list = Group.objects.filter( category__name="互联网/电子商务").order_by("-last_topic_add") # 对群组分页 paginator = Paginator(groups_list, settings.PAGINATION_PER_PAGE) page = request.GET.get('page') try: groups = paginator.page(page) except PageNotAnInteger: groups = paginator.page(1) except EmptyPage: groups = paginator.page(paginator.num_pages) recent_topics = Topic.objects.filter( status='enabled').order_by("-last_reply_add")[:5] categories = Category.objects.filter(parent__isnull=True) # 顶级分类 init_ca_id = Category.objects.get(name="互联网/电子商务").id #初始化的分类的id init_ca_parent_id = Category.objects.get(name="互联网/电子商务").parent.id vt = loader.get_template("index.html") c = RequestContext( request, { 'form': register_form(large_input=False), 'recent_topics': recent_topics, 'categories': categories, 'groups': groups, 'init_ca_id': init_ca_id, 'init_ca_parent_id': init_ca_parent_id, }) return HttpResponse(vt.render(c))
def index(request): """ 首页index """ if request.method == "POST": form = register_form(data=request.POST) if form.is_valid(): form.save() email = form.cleaned_data['email'] user = MyUser.objects.get(email=email) login(request, user) return redirect(reverse('base_info_edit')) # 跳转到基本信息编辑界面 #return redirect('login') # 跳转到登录页面 if request.method == "GET": try: groups_list = Group.objects.only('category').filter(category__id=request.GET["c_id"]).order_by('-last_topic_add')[0:100] except MultiValueDictKeyError: groups_list = Group.objects.only('category').filter(category__name="计算机软件").order_by('-last_topic_add')[0:100] # 对群组分页 paginator = Paginator(groups_list, settings.PAGINATION_PER_PAGE) page = request.GET.get('page') try: groups = paginator.page(page) except PageNotAnInteger: groups = paginator.page(1) except EmptyPage: groups = paginator.page(paginator.num_pages) #recent_topics = Topic.objects.filter(status='enabled').order_by("-last_reply_add")[:5] categories = Category.objects.filter(parent__isnull=True) # 顶级分类 init_ca_id = Category.objects.get(name="计算机软件").id #初始化的分类的id init_ca_parent_id = Category.objects.get(name="计算机软件").parent.id vt = loader.get_template("index.html") c = RequestContext( request, { 'form': register_form(large_input=False), # 'recent_topics': recent_topics, 'categories': categories, 'groups': groups, 'init_ca_id': init_ca_id, 'init_ca_parent_id': init_ca_parent_id, } ) return HttpResponse(vt.render(c))
def test_login_bad_password(self): credentials = { 'email': '*****@*****.**', 'password': '******' # Incorrect password } request = self.create_request(credentials) response = login(request) self.assertEqual(response.status_code, 400, 'Response: {}'.format(response.content))
def main(request): """ 进入网站的页面 未登录的显示,已经登录的跳转到首页 """ if request.user.is_anonymous(): if request.method == 'POST': form = register_form(large_input=False, data=request.POST) if form.is_valid(): form.save() email = form.cleaned_data['email'] user = MyUser.objects.get(email=email) login(request, user) return redirect(reverse('base_info_edit')) # 跳转到基本信息编辑界面 else: form = register_form(large_input=False) ctx = {'form': form} return render(request, 'register.html', ctx) elif request.user.is_authenticated(): return redirect(reverse('index'))
def login(request): if request.method == 'POST': key = views.login(request) if key == 3: return redirect('/') elif key == 4: return render(request, 'loginregister.html', { 'key': 0, 'alert': 0 }) else: return render(request, 'loginregister.html', {'key': 0})
def test_login_with_user(self): """ Test logging in with a valid user. """ # Create an instance of a GET request. request = self.factory.get('/accounts/login') request.user = self.user # Should get a redirect response = login(request) self.assertEqual(response.status_code, 302)
def test_login_no_user(self): """ Test logging in without a valid user. """ # Create an instance of a GET request. request = self.factory.get('/accounts/login') request.user = AnonymousUser() # Test the login view as if it was deployed as 'accounts/login' response = login(request) self.assertEqual(response.status_code, 200)
def test_login_success(self): credentials = { 'email': '*****@*****.**', 'password': '******' } request = self.create_request(credentials) response = login(request) response_data = json.loads(response.content.decode('utf8')) self.assertEqual(response.status_code, 200, 'Response: {}'.format(response.content)) self.assertEqual(response_data['user']['email'], credentials['email'])
def main(request): """ 进入网站的页面 未登录的显示,已经登录的跳转到首页 """ if request.user.is_anonymous(): if request.method == 'POST': form = register_form(large_input=False, data=request.POST) if form.is_valid(): form.save() email = form.cleaned_data['email'] user = MyUser.objects.get(email=email) login(request, user) return redirect(reverse('base_info_edit')) # 跳转到基本信息编辑界面 else: form = register_form(large_input=False) ctx = { 'form': form } return render(request, 'register.html', ctx) elif request.user.is_authenticated(): return redirect(reverse('index'))
def test_login_with_user_redirect(self): """ Test logging in with a redirect """ # Create an instance of a GET request. request = self.factory.get('/accounts/login') request.user = self.user request.GET = QueryDict('next=/') # Should get a redirect response = login(request) self.assertEqual(response.status_code, 302)
def test_login_page_redirect_to_home_when_logged_in(self): request = self.factory.get('/') request.user = self.user page = login(request) self.assertEqual(page.status_code, 302) self.assertEqual(page.url, "/")
def form_valid(self, form): login(self.request, form.cleaned_data) return super(Index, self).form_valid(form)
def topic(self): return login(self.request())
def test_login(self): request = self.factory.get(self.url) request.user = AnonymousUser response = views.login(request) self.assertEqual(response.status_code, 200)