Skip to content

Commit d68b69a

Browse files
committed
Test: Add test/ui/stream_answers__test.py
1 parent 49d0893 commit d68b69a

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

test/ui/stream_answers__test.py

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# -*- coding: utf-8 -*-
2+
import re
3+
import time
4+
5+
from datetime import datetime, date, timedelta
6+
from collections import OrderedDict
7+
8+
import httpx
9+
import pendulum
10+
import pytest
11+
import time_machine
12+
13+
from django.conf import settings
14+
from django.core import mail
15+
from django.urls import reverse
16+
from django.test import TestCase
17+
from django.test import override_settings
18+
from django.utils.timezone import localtime, now
19+
from model_bakery.baker import make
20+
from playwright.sync_api import Playwright, Page, expect
21+
from unittest.mock import Mock, patch, ANY
22+
23+
from ufo.models.base import int16
24+
from ufo.models import (
25+
Region, Country, WebsiteUser, Election, Campaign, Organization, Contact, OrgBranch,
26+
WebsiteUser, QuizTopic, Question, Answer, MobileUser
27+
)
28+
29+
from .. import base
30+
from ..base import MSK, ru, spb, msk, uvicorn_server
31+
32+
33+
# @pytest.mark.asyncio(scope="session")
34+
@pytest.mark.uitest
35+
@pytest.mark.django_db(transaction=True)
36+
@time_machine.travel(datetime(2024, 9, 16, tzinfo=MSK), tick=False)
37+
def stream_answers_scenario__test(uvicorn_server, spb, msk, page):
38+
# # GIVEN current user is logged in as test@example.com
39+
# with base.patch_auth():
40+
# page.goto(f'{uvicorn_server}/auth/login/signed?auth_user=test@example.com')
41+
42+
# AND quiz topic with 2 questions
43+
topic = make(QuizTopic, name='ДО НАЧАЛА ГОЛОСОВАНИЯ', questions=[
44+
make(Question, id='vam_predostavili', label='вам предоставили', type='YESNO'),
45+
make(Question, id='vbros', label='вброс', type='YESNO')
46+
])
47+
48+
# AND Mobile user
49+
appuser = make(MobileUser,
50+
id='123',
51+
# elections=[old_spb_election, guber_spb, president_elections]
52+
)
53+
54+
# AND old Answer from 2016
55+
make(Answer, **{
56+
'appuser': appuser,
57+
'id': 'vam_spb_incident_2016',
58+
'timestamp': datetime(2016, 4, 4, 0, 1, tzinfo=MSK),
59+
'time_created': datetime(2016, 4, 4, 0, 1, tzinfo=MSK),
60+
'question_id': 'vam_predostavili',
61+
'value_bool': False,
62+
'is_incident': True,
63+
'revoked': True,
64+
'uik': 9004,
65+
'role': 'psg',
66+
'uik_complaint_status': int16('отказ принять жалобу'),
67+
'tik_complaint_status': int16('ожидает модератора'),
68+
'tik_complaint_text': '123',
69+
'region': spb
70+
})
71+
72+
# AND recent Answer
73+
make(Answer, **{
74+
'appuser': appuser,
75+
'id': 'vam_spb_incident',
76+
'timestamp': datetime(2024, 9, 16, 0, 1, tzinfo=MSK),
77+
'time_created': datetime(2024, 9, 16, 0, 1, tzinfo=MSK),
78+
'question_id': 'vam_predostavili',
79+
'value_bool': False,
80+
'is_incident': True,
81+
'revoked': True,
82+
'uik': 803,
83+
'role': 'psg',
84+
'uik_complaint_status': int16('отказ принять жалобу'),
85+
'tik_complaint_status': int16('ожидает модератора'),
86+
'tik_complaint_text': '123',
87+
'region': spb
88+
})
89+
90+
# AND recent Answer
91+
make(Answer, **{
92+
'appuser': appuser,
93+
'id': 'vam_spb_ok',
94+
'timestamp': datetime(2024, 9, 16, 0, 2, tzinfo=MSK),
95+
'time_created': datetime(2024, 9, 16, 0, 2, tzinfo=MSK),
96+
'question_id': 'vam_predostavili',
97+
'value_bool': True,
98+
'is_incident': False,
99+
'revoked': False,
100+
'uik': 14,
101+
'role': 'psg',
102+
'uik_complaint_status': int16('не подавалась'),
103+
'tik_complaint_status': int16('ожидает модератора'),
104+
'tik_complaint_text': '123',
105+
'region': spb
106+
})
107+
108+
# AND recent Answer
109+
make(Answer, **{
110+
'appuser': appuser,
111+
'id': 'vbros_a',
112+
'timestamp': datetime(2024, 9, 16, 0, 3, tzinfo=MSK),
113+
'time_created': datetime(2024, 9, 16, 0, 3, tzinfo=MSK),
114+
'question_id': 'vbros',
115+
'value_bool': True,
116+
'is_incident': False,
117+
'revoked': False,
118+
'uik': 7,
119+
'role': 'psg',
120+
'uik_complaint_status': int16('отказ принять жалобу'),
121+
'tik_complaint_status': int16('ожидает модератора'),
122+
'tik_complaint_text': '123',
123+
'region': msk
124+
})
125+
126+
######
127+
# WHEN user opens answers stream
128+
page.goto(f"{uvicorn_server}/answers/stream")
129+
130+
# THEN three recent answers should be visible
131+
expect(page.locator("time").filter(has_text="16 Sep 2024, 00:03")).to_be_visible()
132+
expect(page.locator("time").filter(has_text="16 Sep 2024, 00:02")).to_be_visible()
133+
expect(page.locator("time").filter(has_text="16 Sep 2024, 00:01")).to_be_visible()
134+
# AND old answer should be hidden
135+
expect(page.locator("time").filter(has_text="04 Apr 2016, 00:01")).to_have_count(0)
136+
137+
138+
########
139+
# WHEN user clicks regions filter
140+
page.get_by_placeholder("Region: all").click()
141+
142+
# AND click spb
143+
page.get_by_role("option", name="Санкт-Петербург").click()
144+
145+
# THEN las three spb answers should be visible
146+
expect(page.get_by_text("Санкт-Петербург UIK 14")).to_be_visible()
147+
expect(page.get_by_text("Санкт-Петербург UIK 803")).to_be_visible()
148+
expect(page.get_by_text("Санкт-Петербург UIK 9004")).to_be_visible()
149+
# AND msk answers should be hidden
150+
expect(page.get_by_text("Москва UIK 7")).to_have_count(0)
151+
152+
153+
#####
154+
# WHEN new spb answer is submitted to mobile api
155+
response = httpx.post(f'{uvicorn_server}/api/v4/answers/', json={
156+
'app_id': '123',
157+
'id': '456',
158+
'timestamp': '2025-03-30T23:59:00Z',
159+
'question_id': 'vbros',
160+
'value': True,
161+
'is_incident': True,
162+
'revoked': False,
163+
'uik': 99,
164+
'role': 'psg',
165+
'uik_complaint_status': 'отказ принять жалобу',
166+
'tik_complaint_status': 'отправляется модератору',
167+
'tik_complaint_text': '123',
168+
'region_id': spb.id
169+
})
170+
171+
# THEN response status should be HTTP_201_CREATED
172+
assert response.status_code is 201
173+
174+
# AND new answer should be visible in the stream
175+
expect(page.get_by_text("Санкт-Петербург UIK 99")).to_be_visible()
176+
177+
178+
#####
179+
# WHEN new msk answer is submitted to mobile api
180+
response = httpx.post(f'{uvicorn_server}/api/v4/answers/', json={
181+
'app_id': '123',
182+
'id': '789',
183+
'timestamp': '2025-03-30T23:59:00Z',
184+
'question_id': 'vbros',
185+
'value': True,
186+
'is_incident': True,
187+
'revoked': False,
188+
'uik': 1,
189+
'role': 'psg',
190+
'uik_complaint_status': 'отказ принять жалобу',
191+
'tik_complaint_status': 'отправляется модератору',
192+
'tik_complaint_text': '123',
193+
'region_id': msk.id
194+
})
195+
196+
# THEN response status should be HTTP_201_CREATED
197+
assert response.status_code is 201
198+
199+
# AND new answer should NOT be visible in the stream
200+
expect(page.get_by_text("Москва UIK 1")).to_have_count(0)

0 commit comments

Comments
 (0)