Commit 56561978 authored by Давид Ли's avatar Давид Ли

esdp

parent c0fd7ea9
No preview for this file type
from behave import fixture, use_fixture
from selenium.webdriver import Chrome
@fixture
def browser_chrome(context):
context.browser = Chrome()
yield context.browser
context.browser.quit()
def before_all(context):
use_fixture(browser_chrome, context)
Feature: Вход
Scenario: Вход под админом
Given Я открыл страницу "Входа"
When Я ввожу текст "admin" в поле "username"
And Я ввожу текст "root" в поле "password"
And Я отправляю форму
Then Я должен быть на главной странице
Scenario: Не успешный вход
Given Я открыл страницу "Входа"
When Я ввожу текст "admindsf" в поле "username"
And Я ввожу текст "admindsf" в поле "password"
When Я отправляю форму
Then Я должен быть на странице входа
And Я должен видеть сообщение об ошибке
from behave import given, when, then
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
@given(u'Я открыл страницу "Входа"')
def open_login_page(context):
context.browser.get('http://localhost:1026/auth/login/')
@when(u'Я ввожу текст "{text}" в поле "{name}"')
def enter_text(context, text, name):
context.browser.find_element(By.NAME, name).send_keys(text)
@when(u'Я отправляю форму')
def submit_form(context):
button = context.browser.find_element(By.CLASS_NAME, 'btn')
context.browser.implicitly_wait(10)
ActionChains(context.browser).move_to_element(button).click(button).perform()
@then(u'Я должен быть на главной странице')
def should_be_at_main_page(context):
assert context.browser.current_url == 'http://localhost:1026/articles/'
@then(u'Я должен быть на странице входа')
def should_be_at_login_page(context):
assert context.browser.current_url == 'http://localhost:1026/auth/login/?next='
@then(u'Я должен видеть сообщение об ошибке')
def should_see_error(context):
error = context.browser.find_element(By.CLASS_NAME, 'alert-danger')
assert error.text == 'Пожалуйста, введите правильные имя пользователя и пароль. Оба поля могут быть чувствительны к регистру.'
from django.test import TestCase from django.test import TestCase
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
# Create your tests here.
class LoginTestCase(TestCase):
def setUp(self) -> None:
self.driver = Chrome()
def tearDown(self) -> None:
self.driver.close()
def test_log_in_as_admin(self):
self.get_url('http://localhost:1026/auth/login/')
self.fill_input_by_name('username', 'admin')
self.fill_input_by_name('password', 'root')
self.click_button()
self.check_url('http://localhost:1026/articles/')
def test_log_in_error(self):
self.get_url('http://localhost:1026/auth/login/')
self.fill_input_by_name('username', 'admindsf')
self.fill_input_by_name('password', 'roodasfasdf')
self.click_button()
self.check_url('http://localhost:1026/auth/login/?next=')
self.see_error_with_text('Пожалуйста, введите правильные имя пользователя и пароль. Оба поля могут быть чувствительны к регистру.')
def click_button(self):
button = self.driver.find_element(By.CLASS_NAME, 'btn')
self.driver.implicitly_wait(10)
ActionChains(self.driver).move_to_element(button).click(button).perform()
def get_url(self, url):
self.driver.get(url)
def fill_input_by_name(self, name, text):
self.driver.find_element(By.NAME, name).send_keys(text)
def check_url(self, url):
assert self.driver.current_url == url
def see_error_with_text(self, text):
error = self.driver.find_element(By.CLASS_NAME, 'alert-danger')
assert error.text == text
...@@ -13,13 +13,15 @@ from accounts.models import User, Profile ...@@ -13,13 +13,15 @@ from accounts.models import User, Profile
class AuthSuccessUrlMixin: class AuthSuccessUrlMixin:
def get_success_url(self): def get_success_url(self):
return self.request.GET.get( url = self.request.GET.get('next')
'next',
self.request.POST.get( if not url:
'next', url = self.request.POST.get('next')
reverse_lazy('main_page')
) if not url:
) return reverse_lazy('main_page')
return url
class LoginView(AuthSuccessUrlMixin, views.LoginView): class LoginView(AuthSuccessUrlMixin, views.LoginView):
......
...@@ -15,7 +15,7 @@ class ArticleViewSet(ModelViewSet): ...@@ -15,7 +15,7 @@ class ArticleViewSet(ModelViewSet):
'get': serializers.ArticleGetSerializer, 'get': serializers.ArticleGetSerializer,
'patch': serializers.ArticlePartialUpdateSerializer, 'patch': serializers.ArticlePartialUpdateSerializer,
} }
@decorators.action(methods=['GET'], detail=True) @decorators.action(methods=['GET'], detail=True)
def hello(self, request, *args, **kwargs): def hello(self, request, *args, **kwargs):
return Response({'msg': 'Hello World!'}) return Response({'msg': 'Hello World!'})
......
REDIS_URL = 'redis://redis:6379/0'
\ No newline at end of file
...@@ -40,6 +40,7 @@ INSTALLED_APPS = [ ...@@ -40,6 +40,7 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django_extensions', 'django_extensions',
'behave_django',
'rest_framework', 'rest_framework',
'rest_framework.authtoken', 'rest_framework.authtoken',
...@@ -148,7 +149,7 @@ SHELL_PLUS = "bpython" ...@@ -148,7 +149,7 @@ SHELL_PLUS = "bpython"
AUTH_USER_MODEL = 'accounts.User' AUTH_USER_MODEL = 'accounts.User'
LOGIN_URL = 'login' LOGIN_URL = 'login'
# LOGIN_REDIRECT_URL = 'main_page' LOGIN_REDIRECT_URL = 'main_page'
# LOGOUT_REDIRECT_URL = 'main_page' # LOGOUT_REDIRECT_URL = 'main_page'
faker = Faker() faker = Faker()
......
version: '3'
services:
postgres:
image: postgres:14-alpine
environment:
- POSTGRES_PASSWORD=postgres
app:
build:
dockerfile: docker/Dockerfile
ports:
- "1026:8000"
depends_on:
- postgres
volumes:
- .:/usr/src/app
command: bash -c "python manage.py makemigrations --merge --noinput &&
python manage.py migrate --noinput &&
python manage.py runserver 0.0.0.0:8000"
redis:
image: redis
celery:
build:
dockerfile: docker/Dockerfile
command: celery -A core/celery.py worker -l info
volumes:
- .:/usr/src/app
depends_on:
- postgres
- redis
environment:
- TZ=Asia/Almaty
celery-beat:
build:
dockerfile: docker/Dockerfile
command: celery -A core/celery.py beat -l info
volumes:
- .:/usr/src/app
depends_on:
- postgres
- redis
environment:
- TZ=Asia/Almaty
FROM python:3.10-slim
RUN apt update && apt install -y gcc
ENV PYTHONDONTWRITEBYTECODE 1
WORKDIR /usr/src/app
COPY reqs.txt ./reqs.txt
RUN pip install --upgrade pip && pip install -r reqs.txt
COPY . .
amqp==5.1.1
asgiref==3.7.2 asgiref==3.7.2
billiard==4.1.0
blessed==1.20.0 blessed==1.20.0
bpython==0.24 bpython==0.24
celery==5.3.4
certifi==2023.5.7 certifi==2023.5.7
charset-normalizer==3.2.0 charset-normalizer==3.2.0
click==8.1.7
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.3.0
curtsies==0.4.1 curtsies==0.4.1
cwcwidth==0.1.8 cwcwidth==0.1.8
Django==3.2.19 Django==3.2.19
django-extensions==3.2.3 django-extensions==3.2.3
djangorestframework==3.14.0 djangorestframework==3.14.0
Faker==19.12.0
greenlet==2.0.2 greenlet==2.0.2
idna==3.4 idna==3.4
kombu==5.3.2
Pillow==10.0.0 Pillow==10.0.0
prompt-toolkit==3.0.39
Pygments==2.15.1 Pygments==2.15.1
python-dateutil==2.8.2
pytz==2023.3 pytz==2023.3
pyxdg==0.28 pyxdg==0.28
requests==2.31.0 requests==2.31.0
six==1.16.0 six==1.16.0
sqlparse==0.4.4 sqlparse==0.4.4
typing_extensions==4.8.0
tzdata==2023.3
urllib3==2.0.3 urllib3==2.0.3
vine==5.0.0
wcwidth==0.2.6 wcwidth==0.2.6
from django.test import TestCase from django.test import TestCase
from web.models import Article
from accounts.models import User
# Create your tests here.
class ArticleTestCase(TestCase):
def setUp(self):
admin = User.objects.create(username='admin', password='root')
admin.is_superuser = True
admin.save()
self.client.login(username='admin', password='root')
a1 = Article.objects.create(title='Test 1', text='Test text 1')
a2 = Article.objects.create(title='Test 2', text='Test text 2')
a3 = Article.objects.create(title='Test 3', text='Test text 3')
self.articles = [a1, a2, a3]
def test_update_article_success(self):
article = self.articles[0]
update_data = {'title': 'Article', 'text': 'article text'}
article.title = update_data['title']
article.text = update_data['text']
article.save()
article_from_db = Article.objects.get(id=article.id)
assert article_from_db.title == update_data['title']
assert article_from_db.text == update_data['text']
def test_update_article_error(self):
article = self.articles[0]
update_data = {'title': 'Article', 'text': 'article text'}
article.title = update_data['title']
article.text = update_data['text']
article_from_db = Article.objects.get(id=article.id)
assert not article_from_db.title == update_data['title']
assert not article_from_db.text == update_data['text']
def test_create_article_success(self):
data = {'title': 'title', 'text': 'text'}
response = self.client.post('/api/article/', data=data)
assert response.status_code == 201
Article.objects.update(status='accepted')
assert Article.objects.filter(title=data['title']).exists()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment