def test_init_user(self): """用户初始化测试测试""" init_user(self.u1) self.assertEqual(self.u1, self.resource_group1.users_set.get(pk=self.u1.pk)) # init 需要是无状态的, 可以重复执行, 执行一次和执行n次结果一样 init_user(self.u1) self.assertEqual(self.u1, self.resource_group1.users_set.get(pk=self.u1.pk))
def callback(request): response = request.META["QUERY_STRING"] try: aresp = _client.parse_response(AuthorizationResponse, info=response, sformat="urlencoded") assert aresp["state"] == request.session["state"] resp = _client.do_access_token_request( state=aresp["state"], scope=["profile", "company_info"], request_args={"code": aresp["code"]}, authn_method="client_secret_basic") profile = _client.do_user_info_request(state=aresp["state"]) _logger.debug(base64.b64encode(profile.to_json().encode("utf-8"))) except: return redirect("/login/") email = profile.get("email") if email is None: return render( request, "forbidden.html", { "title": "Sorry, 您暂时无法使用此平台!", "context": "这很奇怪,你好像没有企业邮箱账号☠", "logout": _client.logout_url, }) try: user = Users.objects.get(email=email) except Users.DoesNotExist: return render( request, "forbidden.html", { "title": "您无权访问,如需开通请邮件申请!", "context": """ 邮件格式: 标题: 边锋-数据库平台登陆权限 正文: 姓名: 邮箱: 所属部门: 部门领导: 申请原因: 请发送给雪豹项目部([email protected]),并抄送给部门领导 """, "logout": _client.logout_url, }) if not user.last_login: init_user(user) auth.login(request, user) return redirect("/")