Пример #1
0
    def test_login_connect_and_logout(self):
        a = AuthenticationToken()

        successful_res = mock.NonCallableMock(requests.Response)
        successful_res.status_code = 200
        successful_res.json = mock.MagicMock(
            return_value={"accessToken": "token",
                          "clientToken": "token",
                          "selectedProfile": {
                              "id": "1",
                              "name": "asdf"
                          }}
        )
        successful_res.text = json.dumps(successful_res.json())

        error_res = mock.NonCallableMock(requests.Response)
        error_res.status_code = 400
        error_res.json = mock.MagicMock(
            return_value={
                "error": "invalid request",
                "errorMessage": "invalid request"
            }
        )
        error_res.text = json.dumps(error_res.json())

        def mocked_make_request(server, endpoint, data):
            if endpoint == "authenticate":
                if "accessToken" in data:
                    response = successful_res.copy()
                    response.json["accessToken"] = data["accessToken"]
                    return response
                return successful_res
            if endpoint == "refresh" and data["accessToken"] == "token":
                return successful_res
            if (endpoint == "validate" and data["accessToken"] == "token") \
                    or endpoint == "join":
                r = requests.Response()
                r.status_code = 204
                r.raise_for_status = mock.MagicMock(return_value=None)
                return r
            if endpoint == "signout":
                return successful_res
            if endpoint == "invalidate":
                return successful_res

            return error_res

        # Test a successful sequence of events
        with mock.patch("minecraft.authentication._make_request",
                        side_effect=mocked_make_request) as _make_request_mock:

            self.assertFalse(a.authenticated)
            self.assertTrue(a.authenticate("username", "password"))

            self.assertEqual(_make_request_mock.call_count, 1)
            self.assertIn("clientToken", _make_request_mock.call_args[0][2])

            self.assertTrue(a.authenticated)

            self.assertTrue(a.refresh())
            self.assertTrue(a.validate())

            self.assertTrue(a.authenticated)

            self.assertTrue(a.join(123))
            self.assertTrue(a.sign_out("username", "password"))

            self.assertTrue(a.invalidate())

            self.assertEqual(_make_request_mock.call_count, 6)

        # Test that we send a provided clientToken if the authenticationToken
        # is initialized with one
        with mock.patch("minecraft.authentication._make_request",
                        side_effect=mocked_make_request) as _make_request_mock:
            a = AuthenticationToken(client_token="existing_token")

            self.assertTrue(a.authenticate("username", "password",
                                           invalidate_previous=False))

            self.assertEqual(_make_request_mock.call_count, 1)
            self.assertEqual(
                "existing_token",
                _make_request_mock.call_args[0][2]["clientToken"]
            )

        # Test that we invalidate previous tokens properly
        with mock.patch("minecraft.authentication._make_request",
                        side_effect=mocked_make_request) as _make_request_mock:
            a = AuthenticationToken()

            self.assertFalse(a.authenticated)
            self.assertTrue(a.authenticate("username", "password",
                                           invalidate_previous=True))

            self.assertTrue(a.authenticated)
            self.assertEqual(a.access_token, "token")
            self.assertEqual(_make_request_mock.call_count, 1)
            self.assertNotIn("clientToken", _make_request_mock.call_args[0][2])

        a = AuthenticationToken(username="******",
                                access_token="token",
                                client_token="token")

        # Failures
        with mock.patch("minecraft.authentication._make_request",
                        return_value=error_res) as _make_request_mock:
            self.assertFalse(a.authenticated)

            a.client_token = "token"
            a.access_token = None
            self.assertRaises(ValueError, a.refresh)

            a.client_token = None
            a.access_token = "token"
            self.assertRaises(ValueError, a.refresh)

            a.access_token = None
            self.assertRaises(ValueError, a.validate)

            self.assertRaises(YggdrasilError, a.join, 123)
            self.assertRaises(YggdrasilError, a.invalidate)
Пример #2
0
    def test_login_connect_and_logout(self):
        a = AuthenticationToken()

        successful_res = mock.NonCallableMock(requests.Response)
        successful_res.status_code = 200
        successful_res.json = mock.MagicMock(
            return_value={
                "accessToken": "token",
                "clientToken": "token",
                "selectedProfile": {
                    "id": "1",
                    "name": "asdf"
                }
            })
        successful_res.text = json.dumps(successful_res.json())

        error_res = mock.NonCallableMock(requests.Response)
        error_res.status_code = 400
        error_res.json = mock.MagicMock(return_value={
            "error": "invalid request",
            "errorMessage": "invalid request"
        })
        error_res.text = json.dumps(error_res.json())

        def mocked_make_request(server, endpoint, data):
            if endpoint == "authenticate":
                return successful_res
            if endpoint == "refresh" and data["accessToken"] == "token":
                return successful_res
            if (endpoint == "validate" and data["accessToken"] == "token") \
                    or endpoint == "join":
                r = requests.Response()
                r.status_code = 204
                r.raise_for_status = mock.MagicMock(return_value=None)
                return r
            if endpoint == "signout":
                return successful_res
            if endpoint == "invalidate":
                return successful_res

            return error_res

        # Test a successful sequence of events
        with mock.patch("minecraft.authentication._make_request",
                        side_effect=mocked_make_request) as _make_request_mock:

            self.assertFalse(a.authenticated)
            self.assertTrue(a.authenticate("username", "password"))

            self.assertTrue(a.authenticated)

            self.assertTrue(a.refresh())
            self.assertTrue(a.validate())

            self.assertTrue(a.authenticated)

            self.assertTrue(a.join(123))
            self.assertTrue(a.sign_out("username", "password"))

            self.assertTrue(a.invalidate())

            self.assertEqual(_make_request_mock.call_count, 6)

        a = AuthenticationToken(username="******",
                                access_token="token",
                                client_token="token")

        # Failures
        with mock.patch("minecraft.authentication._make_request",
                        return_value=error_res) as _make_request_mock:
            self.assertFalse(a.authenticated)

            a.client_token = "token"
            a.access_token = None
            self.assertRaises(ValueError, a.refresh)

            a.client_token = None
            a.access_token = "token"
            self.assertRaises(ValueError, a.refresh)

            a.access_token = None
            self.assertRaises(ValueError, a.validate)

            self.assertRaises(YggdrasilError, a.join, 123)
            self.assertRaises(YggdrasilError, a.invalidate)
Пример #3
0
    def test_login_connect_and_logout(self):
        a = AuthenticationToken()

        successful_req = requests.Request()
        successful_req.status_code = 200
        successful_req.json = mock.MagicMock(
            return_value={"accessToken": "token",
                          "clientToken": "token",
                          "selectedProfile": {
                              "id": "1",
                              "name": "asdf"
                          }}
        )

        error_req = requests.Request()
        error_req.status_code = 400
        error_req.json = mock.MagicMock(
            return_value={
                "error": "invalid request",
                "errorMessage": "invalid request"
            }
        )

        def mocked_make_request(server, endpoint, data):
            if endpoint == "authenticate":
                return successful_req
            if endpoint == "refresh" and data["accessToken"] == "token":
                return successful_req
            if (endpoint == "validate" and data["accessToken"] == "token") \
                    or endpoint == "join":
                r = requests.Request()
                r.status_code = 204
                r.raise_for_status = mock.MagicMock(return_value=None)
                return r
            if endpoint == "signout":
                return successful_req
            if endpoint == "invalidate":
                return successful_req

            return error_req

        # Test a successful sequence of events
        with mock.patch("minecraft.authentication._make_request",
                        side_effect=mocked_make_request) as _make_request_mock:

            self.assertFalse(a.authenticated)
            self.assertTrue(a.authenticate("username", "password"))

            self.assertTrue(a.authenticated)

            self.assertTrue(a.refresh())
            self.assertTrue(a.validate())

            self.assertTrue(a.authenticated)

            self.assertTrue(a.join(123))
            self.assertTrue(a.sign_out("username", "password"))

            self.assertTrue(a.invalidate())

            self.assertEqual(_make_request_mock.call_count, 6)

        a = AuthenticationToken(username="******",
                                access_token="token",
                                client_token="token")

        # Failures
        with mock.patch("minecraft.authentication._make_request",
                        return_value=error_req) as _make_request_mock:
            self.assertFalse(a.authenticated)

            a.client_token = "token"
            a.access_token = None
            self.assertRaises(ValueError, a.refresh)

            a.client_token = None
            a.access_token = "token"
            self.assertRaises(ValueError, a.refresh)

            a.access_token = None
            self.assertRaises(ValueError, a.validate)

            self.assertRaises(YggdrasilError, a.join, 123)
            self.assertRaises(YggdrasilError, a.invalidate)