forked from blankoslo/blank.pizza
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added slack endpoint tests * added more tests * added auth tests
- Loading branch information
Showing
5 changed files
with
176 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import pytest | ||
from app.models.user import User | ||
from flask_jwt_extended import create_refresh_token, decode_token | ||
|
||
|
||
@pytest.fixture | ||
def mocked_requests(mocker): | ||
post_mock = mocker.MagicMock() | ||
mocker.patch("requests.post", post_mock) | ||
get_mock = mocker.MagicMock() | ||
mocker.patch("requests.get", get_mock) | ||
yield post_mock, get_mock | ||
|
||
|
||
@pytest.mark.usefixtures('client_class') | ||
class TestAuthSuit: | ||
def test_refresh(self, slack_organizations, users, ): | ||
user = users.get(slack_organizations[0].team_id) | ||
|
||
token = create_refresh_token(identity=user) | ||
headers = {"Authorization": f"Bearer {token}"} | ||
response = self.client.post('/api/auth/refresh', method='post', headers=headers) | ||
decoded_token = decode_token(response.get_json()['access_token']) | ||
assert response.status_code == 200 | ||
assert decoded_token['user']['id'] == user.id | ||
|
||
def test_login(self): | ||
response = self.client.get('/api/auth/login', method='get') | ||
response_data = response.get_json() | ||
auth_url = response_data['auth_url'] | ||
assert response.status_code == 200 | ||
assert isinstance(auth_url, str) | ||
|
||
def test_login_callback_successful(self, db, mocker, mocked_requests, slack_organizations): | ||
slack_organization = slack_organizations[0] | ||
slack_provider_cfg_mock = mocker.MagicMock() | ||
mocker.patch("app.api.auth.get_slack_provider_cfg", slack_provider_cfg_mock) | ||
|
||
auth_client_mock = mocker.MagicMock() | ||
mocker.patch("app.api.auth.auth.client", auth_client_mock) | ||
auth_client_mock.prepare_token_request.return_value = mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock() | ||
auth_client_mock.add_token.return_value = mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock() | ||
|
||
post_mock, get_mock = mocked_requests | ||
token_response = mocker.Mock() | ||
token_response.json.return_value = {"access_token": "fake-token"} | ||
post_mock.return_value = token_response | ||
userinfo_response = mocker.Mock() | ||
userinfo_response.json.return_value = { | ||
"sub": "userId", | ||
"email": "some@email.invalid", | ||
"email_verified": True, | ||
"picture": "https://example.com/picture.jpg", | ||
"given_name": "someName", | ||
"https://slack.com/team_id": slack_organization.team_id, | ||
} | ||
get_mock.return_value = userinfo_response | ||
|
||
response = self.client.get("/api/auth/login/callback?code=abc123") | ||
|
||
assert response.status_code == 200 | ||
assert decode_token(response.json['access_token'])['user']['id'] == "userId" | ||
user = db.session.get(User, "userId") | ||
assert user.email == "some@email.invalid" | ||
assert user.name == "someName" | ||
assert user.slack_organization_id == slack_organization.team_id | ||
|
||
def test_login_callback_email_not_verified(self, mocker, mocked_requests, slack_organizations): | ||
slack_organization = slack_organizations[0] | ||
slack_provider_cfg_mock = mocker.MagicMock() | ||
mocker.patch("app.api.auth.get_slack_provider_cfg", slack_provider_cfg_mock) | ||
|
||
auth_client_mock = mocker.MagicMock() | ||
mocker.patch("app.api.auth.auth.client", auth_client_mock) | ||
auth_client_mock.prepare_token_request.return_value = mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock() | ||
auth_client_mock.add_token.return_value = mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock() | ||
|
||
post_mock, get_mock = mocked_requests | ||
token_response = mocker.Mock() | ||
token_response.json.return_value = {"access_token": "fake-token"} | ||
post_mock.return_value = token_response | ||
userinfo_response = mocker.Mock() | ||
userinfo_response.json.return_value = { | ||
"sub": "userId", | ||
"email": "some@email.invalid", | ||
"email_verified": False, | ||
"picture": "https://example.com/picture.jpg", | ||
"given_name": "someName", | ||
"https://slack.com/team_id": slack_organization.team_id, | ||
} | ||
get_mock.return_value = userinfo_response | ||
|
||
response = self.client.get("/api/auth/login/callback?code=abc123") | ||
|
||
assert response.status_code == 401 | ||
|
||
def test_login_callback_slack_organization_not_installed(self, db, mocker, mocked_requests): | ||
slack_provider_cfg_mock = mocker.MagicMock() | ||
mocker.patch("app.api.auth.get_slack_provider_cfg", slack_provider_cfg_mock) | ||
|
||
auth_client_mock = mocker.MagicMock() | ||
mocker.patch("app.api.auth.auth.client", auth_client_mock) | ||
auth_client_mock.prepare_token_request.return_value = mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock() | ||
auth_client_mock.add_token.return_value = mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock() | ||
|
||
post_mock, get_mock = mocked_requests | ||
token_response = mocker.Mock() | ||
token_response.json.return_value = {"access_token": "fake-token"} | ||
post_mock.return_value = token_response | ||
userinfo_response = mocker.Mock() | ||
userinfo_response.json.return_value = { | ||
"sub": "userId", | ||
"email": "some@email.invalid", | ||
"email_verified": True, | ||
"picture": "https://example.com/picture.jpg", | ||
"given_name": "someName", | ||
"https://slack.com/team_id": "doesntExistTeamId", | ||
} | ||
get_mock.return_value = userinfo_response | ||
|
||
response = self.client.get("/api/auth/login/callback?code=abc123") | ||
|
||
assert response.status_code == 403 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pytest | ||
from app.models.slack_organization import SlackOrganization | ||
|
||
|
||
@pytest.mark.usefixtures('client_class') | ||
class TestSlackSuit: | ||
def test_install(self, app): | ||
response = self.client.get('/api/slack/install', method='get') | ||
response_data = response.get_json() | ||
redirect_url = response_data['redirect_url'] | ||
assert response.status_code == 200 | ||
assert redirect_url == f'https://slack.com/oauth/v2/authorize?scope=channels:read,channels:history,' \ | ||
f'channels:join,channels:manage,groups:read,chat:write,files:read,im:history,im:write,' \ | ||
f'users:read,users:read.email,commands&client_id=' \ | ||
f'{app.config["SLACK_CLIENT_ID"]}&redirect_uri=' \ | ||
f'{app.config["FRONTEND_URI"]}/slack/callback' | ||
|
||
def test_callback(self, db, mock_broker, mocker): | ||
# Mock requests post | ||
requests_mocker = mocker.MagicMock() | ||
mocker.patch('app.api.slack.requests.post', requests_mocker) | ||
# Mock requests post function call return value | ||
requests_post_mock = mocker.MagicMock() | ||
requests_mocker.return_value = requests_post_mock | ||
# Mock requests posts function call json call return value | ||
requests_post_mock.json.return_value = { | ||
'ok': True, | ||
'is_enterprise_install': False, | ||
'team': { | ||
'id': 'dontCareNewTeamId', | ||
'name': 'dontCareNewTeamName' | ||
}, | ||
'app_id': 'dontCareAppId', | ||
'bot_user_id': 'dontCareUserId', | ||
'access_token': 'dontCareAccessToken' | ||
} | ||
|
||
response = self.client.post('/api/slack/callback', method='post', json={'code': 'dontCareCode'}) | ||
|
||
test_slack_organization = db.session.query(SlackOrganization).get('dontCareNewTeamId') | ||
|
||
assert test_slack_organization is not None | ||
mock_broker.send.assert_called() | ||
assert len(mock_broker.send.call_args_list) == 1 | ||
assert mock_broker.send.call_args_list[0].kwargs['body']['type'] == 'new_slack_organization_event' | ||
assert response.status_code == 200 |