Skip to content

Commit

Permalink
tests and rewrite for 2.1 api
Browse files Browse the repository at this point in the history
Signed-off-by: viste <[email protected]>
  • Loading branch information
Viste committed Aug 20, 2024
1 parent 1ce3fff commit 97347d1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 12 deletions.
24 changes: 22 additions & 2 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
name: deploy site
name: deploy and test site

on:
push:
branches: [ 'master' ]

pull_request:
branches: [ 'master' ]

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.12' # Specify the Python version your project uses

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest pytest-mock
- name: Run tests
run: pytest tests/

build-image:
runs-on: ubuntu-latest
needs: test
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand Down
19 changes: 9 additions & 10 deletions core/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

oauth = OAuth()

print(f'VK_CLIENT_ID: {Config.VK_CLIENT_ID}')
print(f'VK_CLIENT_SECRET: {Config.VK_CLIENT_SECRET}')


def setup_routes(app):
# Настройка OAuth для VK
oauth.init_app(app)
vk = oauth.register(
name='vk',
client_id=Config.VK_CLIENT_ID,
client_secret=Config.VK_CLIENT_SECRET,
authorize_url='https://oauth.vk.com/authorize',
access_token_url='https://oauth.vk.com/access_token',
client_kwargs={'scope': 'email'}
authorize_url='https://id.vk.com/auth',
access_token_url='https://id.vk.com/token',
client_kwargs={
'scope': 'email',
'token_endpoint_auth_method': 'client_secret_post',
'token_placement': 'header',
'response_type': 'code'
},
)

@app.route('/')
Expand Down Expand Up @@ -177,7 +177,6 @@ def authorize_vk():
profile_picture = profile.get('photo_100', '')
email = token.get('email')

# Авторизуем пользователя в системе
authenticate_vk_user(vk_id, screen_name, first_name, last_name, profile_picture, email)

flash(f'Successfully logged in as {screen_name}', 'success')
Expand All @@ -192,4 +191,4 @@ def authorize_vk():
@login_required
def logout():
logout_user()
return redirect(url_for('index'))
return redirect(url_for('index'))
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ python-dotenv
Authlib
Flask-OAuthlib
requests-oauthlib
pytest
pytest-mock
71 changes: 71 additions & 0 deletions tests/test_vk_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from unittest.mock import patch

import pytest

from app import app


@pytest.fixture
def client():
app.config['TESTING'] = True
with app.test_client() as client:
with app.app_context():
yield client


def test_login_vk_redirect(client):
response = client.get('/login/vk')
assert response.status_code == 302


@patch('app.oauth.vk.authorize_access_token')
@patch('app.oauth.vk.get')
def test_authorize_vk_success(mock_get, mock_authorize_access_token, client):
mock_authorize_access_token.return_value = {
'access_token': 'mock_access_token',
'email': '[email protected]'
}

mock_get.return_value.json.return_value = {
'response': [{
'id': 12345,
'first_name': 'John',
'last_name': 'Doe',
'screen_name': 'john_doe',
'photo_100': 'http://example.com/photo.jpg'
}]
}

with patch('tools.auth.authenticate_vk_user') as mock_authenticate:
response = client.get('/vk/callback')

assert response.status_code == 302 # Redirects after successful login
assert mock_authenticate.called_once_with(
12345, 'john_doe', 'John', 'Doe', 'http://example.com/photo.jpg', '[email protected]'
)


@patch('app.oauth.vk.authorize_access_token')
def test_authorize_vk_no_token(mock_authorize_access_token, client):
mock_authorize_access_token.return_value = None

response = client.get('/vk/callback')

assert response.status_code == 302
assert b'login' in response.location


@patch('app.oauth.vk.authorize_access_token')
@patch('app.oauth.vk.get')
def test_authorize_vk_profile_failure(mock_get, mock_authorize_access_token, client):
mock_authorize_access_token.return_value = {
'access_token': 'mock_access_token',
'email': '[email protected]'
}

mock_get.return_value.json.return_value = {'response': []}

response = client.get('/vk/callback')

assert response.status_code == 302
assert b'login' in response.location

0 comments on commit 97347d1

Please sign in to comment.