def test_ping_u1_url(self, mock_json_loads, mock_gtk, Client, get_ping_info): from_page = 1 email = 'email' signed_url = "signed_url" signed_headers = {'a': 'b'} Client.return_value.sign.return_value = (signed_url, signed_headers, None) get_ping_info.return_value = ('url', {'C': 'D'}) mock_json_loads.return_value = {'consumer_key': 'ck', 'consumer_secret': 'cs', 'token_key': 'tk', 'token_secret': 'ts'} with patch.multiple(self.page, soup=DEFAULT, session=DEFAULT, oauth_token_json=sentinel.token) as mocks: self.page.ping_u1_url(email, from_page) mock_json_loads.assert_called_once_with(sentinel.token) sigtype = oauthlib.oauth1.SIGNATURE_TYPE_AUTH_HEADER expected = [call('ck', 'cs', 'tk', 'ts', signature_method=oauthlib.oauth1.SIGNATURE_HMAC, signature_type=sigtype), call().sign('url?C=D', 'GET')] self.assertEqual(Client.mock_calls, expected) expected = [call.Message.new("GET", signed_url), call.Message.new().request_headers.append('a', 'b')] self.assertEqual(mocks['soup'].mock_calls, expected) info = {'action': ubi_ubuntuone.PING_CALLBACK_ACTION, 'from_page': from_page} e = [call.queue_message(mocks['soup'].Message.new.return_value, self.page._handle_soup_message_done, info)] self.assertEqual(mocks['session'].mock_calls, e)
def test_login_to_sso(self, mock_json_dumps, mock_gtk): email = 'email' password = '******' token_name = 'tok' json_ct = 'application/json' expected_dict = {'email': email, 'password': password, 'token_name': token_name} # NOTE: in order to avoid failing tests when dict key ordering # changes, we pass the actual dict by mocking json.dumps. This # way we can compare the dicts instead of their # serializations. mock_json_dumps.return_value = expected_dict with patch.multiple(self.page, soup=DEFAULT, session=DEFAULT) as mocks: typeobj = type(mocks['soup'].MemoryUse) typeobj.COPY = PropertyMock(return_value=sentinel.COPY) self.page.login_to_sso(email, password, token_name, ubi_ubuntuone.PAGE_LOGIN) expected = [call.Message.new("POST", ubi_ubuntuone.UBUNTU_SSO_URL + 'tokens/oauth'), call.Message.new().set_request(json_ct, sentinel.COPY, expected_dict, len(expected_dict)), call.Message.new().request_headers.append('Accept', json_ct)] self.assertEqual(mocks['soup'].mock_calls, expected) info = {'action': ubi_ubuntuone.TOKEN_CALLBACK_ACTION, 'from_page': ubi_ubuntuone.PAGE_LOGIN} e = [call.queue_message(mocks['soup'].Message.new.return_value, self.page._handle_soup_message_done, info)] self.assertEqual(mocks['session'].mock_calls, e)
def test_register_new_sso_account(self, mock_json_dumps, mock_gtk): email = 'email' password = '******' displayname = 'mr tester' json_ct = 'application/json' expected_dict = {'email': email, 'displayname': displayname, 'password': password} # See test_login_to_sso for comment about patching json.dumps(): mock_json_dumps.return_value = expected_dict with patch.multiple(self.page, soup=DEFAULT, session=DEFAULT) as mocks: typeobj = type(mocks['soup'].MemoryUse) typeobj.COPY = PropertyMock(return_value=sentinel.COPY) self.page.register_new_sso_account(email, password, displayname) expected = [call.Message.new("POST", ubi_ubuntuone.UBUNTU_SSO_URL + 'accounts'), call.Message.new().set_request(json_ct, sentinel.COPY, expected_dict, len(expected_dict)), call.Message.new().request_headers.append('Accept', json_ct)] self.assertEqual(mocks['soup'].mock_calls, expected) info = {'action': ubi_ubuntuone.ACCOUNT_CALLBACK_ACTION, 'from_page': ubi_ubuntuone.PAGE_REGISTER} e = [call.queue_message(mocks['soup'].Message.new.return_value, self.page._handle_soup_message_done, info)] self.assertEqual(mocks['session'].mock_calls, e)