def test_agent(self): payload = { "username": "******", "password": "******", "agent": {"name": "Minecraft", "version": 1} } req.Authenticate(json.dumps(payload)) payload["agent"]["name"] = 123 with self.assertRaises(exc.BadPayload): req.Authenticate(json.dumps(payload))
def test_empty_credentials(self): with self.assertRaises(exc.EmptyCredentials): payload = json.dumps({}) req.Authenticate(payload) with self.assertRaises(exc.EmptyCredentials): payload = json.dumps({"username": "******"}) req.Authenticate(payload) with self.assertRaises(exc.EmptyCredentials): payload = json.dumps({"password": "******"}) req.Authenticate(payload)
def test_client_token(self): payload = { "username": "******", "password": "******", "clientToken": generate_token() } req.Authenticate(json.dumps(payload)) payload["clientToken"] = "" with self.assertRaises(exc.BadPayload): req.Authenticate(json.dumps(payload)) payload["clientToken"] = {12: 34} with self.assertRaises(exc.BadPayload): req.Authenticate(json.dumps(payload))
def test_success_refresh(self): payload = { "username": "******", "password": "******", } request = req.Authenticate(json.dumps(payload)) request.process() result = request.result token = Token.query.filter(Token.client == result["clientToken"]).one() old_token_values = (token.access, token.client) refresh_request = req.Refresh(json.dumps(result)) refresh_request.process() refresh_result = refresh_request.result fresh_token = ( Token.query .filter(Token.client == refresh_result["clientToken"]) .one() ) new_token_values = (token.access, token.client) self.assertNotEqual(result, fresh_token) self.assertNotEqual(old_token_values, new_token_values) self.assertEqual(refresh_result["accessToken"], token.access) self.assertEqual(refresh_result["clientToken"], token.client)
def test_wrong_password(self): payload = { "username": "******", "password": "******", } with self.assertRaises(exc.InvalidCredentials): request = req.Authenticate(json.dumps(payload)) request.process()
def test_no_such_user(self): payload = { "username": "******", "password": "******", } with self.assertRaises(exc.InvalidCredentials): request = req.Authenticate(json.dumps(payload)) request.process()
def test_migration_done(self): payload = { "username": "******", "password": "******", } with self.assertRaises(exc.MigrationDone): request = req.Authenticate(json.dumps(payload)) request.process()
def test_nonstring_credentials(self): values = (123, True, [1], {2: 3}) users = product(["username"], values) passwords = product(["password"], values) pairs = list(product(list(users), list(passwords))) probes = [dict(pair) for pair in pairs] for pair in probes: with self.assertRaises(exc.InvalidCredentials): req.Authenticate(json.dumps(pair))
def test_successful_validate(self): payload = { "username": "******", "password": "******" } request = req.Authenticate(json.dumps(payload)) request.process() payload = {"accessToken": request.result["accessToken"]} request = req.Validate(json.dumps(payload)) request.process()
def test_success_custom_client_token(self): payload = { "username": "******", "password": "******", "clientToken": generate_token() } request = req.Authenticate(json.dumps(payload)) request.process() result = request.result self.assertIn("clientToken", result) self.assertEqual(payload["clientToken"], result["clientToken"])
def authenticate(raw_data): """Authenticate endpoint .. note:: URL: /authenticate :type raw_data: bytes :rtype: str :return: JSON-encoded dict """ instance = req.Authenticate(raw_data) instance.process() return instance.result
def test_success_simple(self): payload = { "username": "******", "password": "******", } request = req.Authenticate(json.dumps(payload)) request.process() result = request.result user = User.query.filter(User.email == payload["username"]).one() self.assertIn("accessToken", result) self.assertIn("clientToken", result) self.assertEqual(result["accessToken"], user.token.access) self.assertEqual(result["clientToken"], user.token.client)
def test_successful_invalidate(self): payload = { "username": "******", "password": "******" } user = User.query.filter(User.email == payload["username"]).one() token_id = user.token.id request = req.Authenticate(json.dumps(payload)) request.process() result = request.result invalidate_request = req.Invalidate(json.dumps(result)) invalidate_request.process() self.assertIsNone(user.token) self.assertIsNone(Token.query.get(token_id))
def test_success_agent_profiles(self): payload = { "username": "******", "password": "******", "agent": {"name": "Minecraft", "version": 1} } request = req.Authenticate(json.dumps(payload)) request.process() result = request.result user = User.query.filter(User.email == payload["username"]).one() profile = {"id": user.token.client, "name": user.login} self.assertIn("selectedProfile", result) self.assertEqual(result["selectedProfile"], profile) self.assertIn("availableProfiles", result) self.assertEqual(result["availableProfiles"], [profile])