def test_get_with_malformed_json_credentials_stored(self): with mock.patch.object(keyring, 'get_password', return_value='{', autospec=True) as get_password: store = Storage('my_unit_test', 'me') credentials = store.get() self.assertEquals(None, credentials) get_password.assert_called_once_with('my_unit_test', 'me')
def test_locked_delete(self): service_name = 'my_unit_test' user_name = 'me' store = Storage(service_name, user_name) with mock.patch.object(keyring, 'set_password', return_value=None, autospec=True) as set_password: store.locked_delete() set_password.assert_called_once_with(service_name, user_name, '')
def sync_calender(): # check storage for credentials store = Storage('GCal', frappe.session.user) # store = Storage('GCal', "makarand") credentials = store.get() if not credentials or credentials.invalid: url = get_oauth2_authorize_url('gcal') return {"url": url, "is_synced": False} else: from gcal.tasks import sync_google_calendar sync_google_calendar(credentials) return {"url": None, "is_synced": True}
def test_locked_put(self): service_name = 'my_unit_test' user_name = 'me' store = Storage(service_name, user_name) with mock.patch.object(keyring, 'set_password', return_value=None, autospec=True) as set_password: credentials = mock.MagicMock() to_json_ret = object() credentials.to_json = to_json = mock.MagicMock( name='to_json', return_value=to_json_ret) store.locked_put(credentials) to_json.assert_called_once_with() set_password.assert_called_once_with(service_name, user_name, to_json_ret)
def test_constructor(self): service_name = 'my_unit_test' user_name = 'me' store = Storage(service_name, user_name) self.assertEqual(store._service_name, service_name) self.assertEqual(store._user_name, user_name) lock_type = type(threading.Lock()) self.assertTrue(isinstance(store._lock, lock_type))
def getAuthToken(client_id, client_secret): # Check if stored credentials are valid storage = Storage('Datamaster', 'user1') credential = storage.get() # Check if credential are still valid if (credential is None or credential.invalid): # Credentials expired -> Get new one # Create Flow object to handle OAuth 2.0 flow = OAuth2WebServerFlow(client_id=client_id, client_secret=client_secret, scope='https://www.googleapis.com/auth/drive', redirect_uri='urn:ietf:wg:oauth:2.0:oob') # Get and store new credential credential = tools.run_flow(flow, storage) return credential
def test_locked_get(self): service_name = 'my_unit_test' user_name = 'me' mock_content = (object(), 'mock_content') mock_return_creds = mock.MagicMock() mock_return_creds.set_store = set_store = mock.MagicMock( name='set_store') with mock.patch.object(keyring, 'get_password', return_value=mock_content, autospec=True) as get_password: class_name = 'oauth2client.contrib.keyring_storage.Credentials' with mock.patch(class_name) as MockCreds: MockCreds.new_from_json = new_from_json = mock.MagicMock( name='new_from_json', return_value=mock_return_creds) store = Storage(service_name, user_name) credentials = store.locked_get() new_from_json.assert_called_once_with(mock_content) get_password.assert_called_once_with(service_name, user_name) self.assertEqual(credentials, mock_return_creds) set_store.assert_called_once_with(store)
def get_credentials(code): if code: params = get_oauth_keys('gcal') params.update({ "scope": 'https://www.googleapis.com/auth/calendar', "redirect_uri": get_redirect_uri('gcal'), "params": { "approval_prompt": "force", 'access_type': 'offline', "response_type": "code" } }) flow = OAuth2WebServerFlow(**params) credentials = flow.step2_exchange(code) # Store Credentials in Keyring Storage store = Storage('GCal', frappe.session.user) store.put(credentials) # get events and create new doctype from gcal.tasks import sync_google_calendar sync_google_calendar(credentials) frappe.local.response["type"] = "redirect" frappe.local.response["location"] = "/desk#Calendar/Event"
def test_get_and_set_with_json_credentials_stored(self): access_token = 'foo' client_id = 'some_client_id' client_secret = 'cOuDdkfjxxnv+' refresh_token = '1/0/a.df219fjls0' token_expiry = datetime.datetime.utcnow() user_agent = 'refresh_checker/1.0' credentials = OAuth2Credentials( access_token, client_id, client_secret, refresh_token, token_expiry, GOOGLE_TOKEN_URI, user_agent) # Setting autospec on a mock with an iterable side_effect is # currently broken (http://bugs.python.org/issue17826), so instead # we patch twice. with mock.patch.object(keyring, 'get_password', return_value=None, autospec=True) as get_password: with mock.patch.object(keyring, 'set_password', return_value=None, autospec=True) as set_password: store = Storage('my_unit_test', 'me') self.assertEquals(None, store.get()) store.put(credentials) set_password.assert_called_once_with( 'my_unit_test', 'me', credentials.to_json()) get_password.assert_called_once_with('my_unit_test', 'me') with mock.patch.object(keyring, 'get_password', return_value=credentials.to_json(), autospec=True) as get_password: restored = store.get() self.assertEqual('foo', restored.access_token) self.assertEqual('some_client_id', restored.client_id) get_password.assert_called_once_with('my_unit_test', 'me')
def test_release_lock(self): store = Storage('my_unit_test', 'me') store._lock = lock = _FakeLock() self.assertEqual(lock._release_count, 0) store.release_lock() self.assertEqual(lock._release_count, 1)
def get_storage(self): return Storage( "cactus/gcs", self.engine.bucket_name ) #TODO: Not a great key, but do we want to ask for email?