def test_validate_invalid_schema(): """Test that an invalid message throws an exception.""" data = { "access_token": None, "expires_in": 2, "scope": "mock_scope", "refresh_token": "mock_refresah", "token_type": "bearer", } with pytest.raises(exceptions.InvalidMessageError): validation.validate(validation.AUTH_TOKEN, data)
def create_session_from_oauth_code(self, code, state): """Create a session object authenticated by an oauth code. :param code: Auth code received from Automatic redirect URL GET :param state: State received from Automatic redirect URL GET :returns Session: Authenticated session object """ if state != self.state: raise exceptions.StateError( "Error creating session. State {} returned by OAauth2 request " "doesn't match aioautomatic state {}. A possible forgery " "attack is taking place. See " "https://developer.automatic.com/api-reference/#oauth-workflow" .format(state, self.state)) _LOGGER.info("Creating session from oauth code.") auth_payload = { 'client_id': self._client_id, 'client_secret': self._client_secret, 'grant_type': 'authorization_code', 'code': code, } resp = yield from self._post(const.AUTH_URL, auth_payload) data = validation.validate(validation.AUTH_TOKEN, resp) return session.Session(self, **data)
def __init__(self, parent, resp, item_class): """Create a result list object.""" BaseApiObject.__init__(self, parent) self._item_class = item_class resp = validation.validate(validation.LIST_RESPONSE, resp) list.__init__(self, (item_class(item) for item in resp['results'])) self._next = resp['_metadata']['next'] self._previous = resp['_metadata']['previous']
def test_validate_schema(): """Test that a valid message returns the correct object.""" data = { "access_token": "mock_token", "expires_in": 2, "scope": "mock_scope", "refresh_token": "mock_refresah", "token_type": "bearer", } validated = validation.validate(validation.AUTH_TOKEN, data) assert data == validated
def create_session_from_refresh_token(self, refresh_token): """Create a session object authenticated by a stored refresh token. :param refresh_token: Refresh token from previous session :returns Session: Authenticated session object """ _LOGGER.info("Creating session from refresh token.") auth_payload = { 'client_id': self._client_id, 'client_secret': self._client_secret, 'grant_type': 'refresh_token', 'refresh_token': refresh_token, } resp = yield from self._post(const.AUTH_URL, auth_payload) data = validation.validate(validation.AUTH_TOKEN, resp) return session.Session(self, **data)
def __init__(self, data): """Create the data object.""" self._data = validation.validate(self.validator, data)