def test_remove_non_existent_is_silent(): team_id = "team" user_id = "user" expected_token = "foobar" gateway = StubUserTokenStore() expected_user = User(user_id=user_id, team_id=team_id, token=expected_token) unstored_user = User(user_id="foo", team_id="bar", token=None) gateway.store(expected_user) assert gateway.remove(unstored_user) is False
def test_links_with_user_object(): gateway = StubUserLinkStore() user1 = User("user1", "team1") user2 = User("user2", "team2") user3 = User("user3", "team3") user4 = User("user4", "team4") gateway.link(user1, user2) gateway.link(user2, user3) gateway.link(user3, user4) assert gateway.fetch(user1) == {user2, user3, user4} assert gateway.fetch(user2) == {user1, user3, user4} assert gateway.fetch(user3) == {user1, user2, user4} assert gateway.fetch(user4) == {user1, user2, user3}
def execute(self, update_event): event = UpdateEventReader(update_event) logging.debug( "update event of user: '******' USERID: '%s' TEAMID: '%s'" " to status text: '%s' emoji '%s' expiration: '%s'", event.name, event.user_id, event.team_id, event.status_text, event.status_emoji, event.status_expiration, ) try: user_list = self.user_link_store.fetch( User(event.user_id, event.team_id)) for user in user_list: user_with_token = self.user_token_store.fetch(user) slack.update_status( token=user_with_token.token, status_text=event.status_text, status_emoji=event.status_emoji, status_expiration=event.status_expiration, ) except KeyError: pass
def test_user_install_stores_token_if_success(mocker): expected_user = User(team_id="foo-team", user_id="foo-user") mocker.patch( "slack_profile_update.gateway.slack.authorisation_grant", return_value=AuthorisationGrantResponse( success=True, team=expected_user.team_id, user=expected_user.user_id, token="foo-token", scope=EXPECTED_SCOPE, ), ) client_id = "test client id" client_secret = "test client secret" stub_user_token_store = StubUserTokenStore() user_install = UserInstall( client_id=client_id, client_secret=client_secret, redirect_uri="example.com", user_token_store=stub_user_token_store, ) response = user_install.execute("foobar", "test-state") assert response.present()["statusCode"] == 200 assert stub_user_token_store.fetch(expected_user).token == "foo-token"
def execute(self, event_map): event = TokenRevokedReader(event_map) for user_id in event.user_ids: user = User(user_id=user_id, team_id=event.team_id) self.__user_link_store.unlink(user) user_removed = self.__user_token_store.remove(user) if user_removed: logging.info("uninstalled user") else: logging.warning("tried to uninstall user that did not exist")
def test_user_uninstall_removed_users_from_gateways(test_file): event = test_file("user_token_revoked.json") secret = "secret" user_to_be_removed = User("U019LN451HT", "T019PQN3UAE", "token1") linked_user_1 = User("user1", "team1", "token2") linked_user_2 = User("user2", "team1", "token3") link_store = StubUserLinkStore() link_store.link(user_to_be_removed, linked_user_1) link_store.link(user_to_be_removed, linked_user_2) token_store = StubUserTokenStore() token_store.store(user_to_be_removed) token_store.store(linked_user_1) token_store.store(linked_user_2) response = HandleEvent( environment={ "SLACK_SIGNING_SECRET": secret }, headers=event_signature_headers(secret, event), raw_body=event, user_link_store=link_store, user_token_store=token_store, ).execute() assert response.present() == { "statusCode": 204, "headers": {}, "body": None, } with pytest.raises(KeyError): token_store.fetch(user_to_be_removed) with pytest.raises(KeyError): link_store.fetch(user_to_be_removed) # does not remove linked users token_store.fetch(linked_user_1) token_store.fetch(linked_user_2) link_store.fetch(linked_user_2) link_store.fetch(linked_user_2)
def test_user_uninstall_logs_message(caplog, test_file): event = json.loads(test_file("user_token_revoked.json")) user_to_be_removed = User("U019LN451HT", "T019PQN3UAE", "token1") token_store = StubUserTokenStore() token_store.store(user_to_be_removed) with caplog.at_level(logging.INFO): UserUninstall(user_link_store=StubUserLinkStore(), user_token_store=token_store).execute(event) assert "uninstalled user" in caplog.text, "missing log entry"
def test_updates_status_of_linked_users(caplog, test_file, mocker): mocker.patch( "slack_profile_update.gateway.slack.update_status", return_value=True, ) event = json.loads(test_file("example_user_updated_event.json")) source_user = User("U019LN451HT", "T019PQN3UAE", "token1") dest_user_1 = User("user1", "team1", "token2") dest_user_2 = User("user3", "team3", "token3") link_store = StubUserLinkStore() link_store.link(source_user, dest_user_1) link_store.link(source_user, dest_user_2) token_store = StubUserTokenStore() token_store.store(dest_user_1) token_store.store(dest_user_2) UpdateAllProfiles(user_link_store=link_store, user_token_store=token_store).execute(event) slack.update_status.assert_has_calls( [ call( status_emoji=":smile:", status_expiration=0, status_text="This is a test!", token=dest_user_1.token, ), call( status_emoji=":smile:", status_expiration=0, status_text="This is a test!", token=dest_user_2.token, ), ], any_order=True, )
def test_remove_a_user_token(): team_id = "team" user_id = "user" expected_token = "foobar" gateway = StubUserTokenStore() expected_user = User(user_id=user_id, team_id=team_id, token=expected_token) gateway.store(expected_user) gateway.fetch(expected_user) assert gateway.remove(expected_user) is True with pytest.raises(KeyError): gateway.fetch(expected_user)
def test_can_get_a_user_token(): team_id = "team" user_id = "user" expected_token = "foobar" gateway = StubUserTokenStore() expected_user = User(user_id=user_id, team_id=team_id, token=expected_token) gateway.store(expected_user) user = gateway.fetch(expected_user) assert expected_user == user assert expected_token == user.token
def execute(self, code, state): logging.debug( "received authorization_grant code '%s'", code, ) logging.debug( "received authorization_grant state '%s'", state, ) gateway_response = slack.authorisation_grant( client_id=self.__client_id, client_secret=self.__client_secret, code=code, redirect_uri=self.__redirect_uri, ) response = ApiGatewayResponse() if not gateway_response.success: logging.warning("returning auth error due to gateway failure") return response.auth_error() if gateway_response.scope == EXPECTED_SCOPE: body = RedirectUriPageRenderer( install_path="", redirect_uri_path="" ).render_success_page(app_id="fakeappid", team_id=None) user = User( team_id=gateway_response.team, user_id=gateway_response.user, token=gateway_response.token, ) self.__user_token_store.store(user) return response.ok_html(body) else: logging.warning( f"scope differs from expected scope {gateway_response.scope} != {EXPECTED_SCOPE}" ) return response.auth_error()
def test_can_store_a_user_token(): StubUserTokenStore().store(User("team", "user", "test-token"))
def fetch(self, user): token = self.user_tokens[f"{user.team_id}-|-{user.user_id}"] return User(user_id=user.user_id, team_id=user.team_id, token=token)