def test_disqus_sso_payload_auth_user(self, mock_b64encode, mock_hmac): """Test Disqus SSO payload auth works.""" user = UserFactory.create() DISQUS_PUBLIC_KEY = 'public' DISQUS_SECRET_KEY = 'secret' patch_dict = { 'DISQUS_PUBLIC_KEY': DISQUS_PUBLIC_KEY, 'DISQUS_SECRET_KEY': DISQUS_SECRET_KEY } data = json.dumps({ 'id': user.id, 'username': user.name, 'email': user.email_addr }) mock_b64encode.return_value = data with patch.dict(self.flask_app.config, patch_dict): message, timestamp, sig, pub_key = util.get_disqus_sso_payload( user) mock_b64encode.assert_called_with(data) mock_hmac.assert_called_with(DISQUS_SECRET_KEY, '%s %s' % (data, timestamp), hashlib.sha1) assert timestamp assert sig assert pub_key == DISQUS_PUBLIC_KEY
def test_disqus_sso_payload_anon_user_no_keys(self): """Test Disqus SSO without keys anon works.""" message, timestamp, sig, pub_key = util.get_disqus_sso_payload(None) assert message is None assert timestamp is None assert sig is None assert pub_key is None
def get_disqus_sso_api(): """Return remote_auth_s3 and api_key for disqus SSO.""" try: if current_user.is_authenticated: message, timestamp, sig, pub_key = get_disqus_sso_payload(current_user) else: message, timestamp, sig, pub_key = get_disqus_sso_payload(None) if message and timestamp and sig and pub_key: remote_auth_s3 = "%s %s %s" % (message, sig, timestamp) tmp = dict(remote_auth_s3=remote_auth_s3, api_key=pub_key) return Response(json.dumps(tmp), mimetype='application/json') else: raise MethodNotAllowed except MethodNotAllowed as e: e.message = "Disqus keys are missing" return error.format_exception(e, target='DISQUS_SSO', action='GET')
def get_disqus_sso_api(): """Return remote_auth_s3 and api_key for disqus SSO.""" try: if current_user.is_authenticated(): message, timestamp, sig, pub_key = get_disqus_sso_payload(current_user) else: message, timestamp, sig, pub_key = get_disqus_sso_payload(None) if message and timestamp and sig and pub_key: remote_auth_s3 = "%s %s %s" % (message, sig, timestamp) tmp = dict(remote_auth_s3=remote_auth_s3, api_key=pub_key) return Response(json.dumps(tmp), mimetype='application/json') else: raise MethodNotAllowed except MethodNotAllowed as e: e.message = "Disqus keys are missing" return error.format_exception(e, target='DISQUS_SSO', action='GET')
def test_disqus_sso_payload_auth_user_no_keys(self, mock_b64encode, mock_hmac): """Test Disqus SSO without keys works.""" user = UserFactory.create() message, timestamp, sig, pub_key = util.get_disqus_sso_payload(user) assert message is None assert timestamp is None assert sig is None assert pub_key is None
def test_disqus_sso_payload_anon_user(self, mock_b64encode, mock_hmac): """Test Disqus SSO payload anon works.""" DISQUS_PUBLIC_KEY = 'public' DISQUS_SECRET_KEY = 'secret' patch_dict = {'DISQUS_PUBLIC_KEY': DISQUS_PUBLIC_KEY, 'DISQUS_SECRET_KEY': DISQUS_SECRET_KEY} data = json.dumps({}) mock_b64encode.return_value = data with patch.dict(self.flask_app.config, patch_dict): message, timestamp, sig, pub_key = util.get_disqus_sso_payload(None) mock_b64encode.assert_called_with(data) mock_hmac.assert_called_with(DISQUS_SECRET_KEY, '%s %s' % (data, timestamp), hashlib.sha1) assert timestamp assert sig assert pub_key == DISQUS_PUBLIC_KEY
def test_disqus_sso_payload_auth_user(self, mock_b64encode, mock_hmac): """Test Disqus SSO payload auth works.""" user = UserFactory.create() DISQUS_PUBLIC_KEY = 'public' DISQUS_SECRET_KEY = 'secret' patch_dict = {'DISQUS_PUBLIC_KEY': DISQUS_PUBLIC_KEY, 'DISQUS_SECRET_KEY': DISQUS_SECRET_KEY} data = json.dumps({'id': user.id, 'username': user.name, 'email': user.email_addr}) mock_b64encode.return_value = data with patch.dict(self.flask_app.config, patch_dict): message, timestamp, sig, pub_key = util.get_disqus_sso_payload(user) mock_b64encode.assert_called_with(data) mock_hmac.assert_called_with(DISQUS_SECRET_KEY, '%s %s' % (data, timestamp), hashlib.sha1) assert timestamp assert sig assert pub_key == DISQUS_PUBLIC_KEY