def test_add_two_users_with_same_email(self, api_client: ApiClient): """Test that it is impossible to create two users with same email Steps: - Make request to application with path /api/add_user and JSON in valid format - Make another request to application with path /api/add_user but different username and password Expected results: Status code for the first request is in 200-300 range Status code for the first request is in 400-500 range """ username, email, password = make.auth_data() username2, _, password2 = make.auth_data(email=email) with allure.step( 'Add the first user and check status code is in range 200 to 300' ): response = api_client.add_user(username=username, email=email, password=password) attach_http(response, '1') assert response.status_code in range(200, 300) with allure.step( 'Add the second user and check status code is in range 400 to 500' ): response2 = api_client.add_user(username=username2, email=email, password=password2) attach_http(response2, '2') assert response2.status_code in range(400, 500)
def test_invalid_json(self, json, api_client: ApiClient): """Test status codes for requests with different invalid JSON Steps: - Make request to application with path /api/add_user and JSON in invalid formats Expected results: Response status code is 400 """ with allure.step('Send invalid json to add user'): response = requests.post( f'http://{api_client.netloc}/api/add_user', cookies=api_client.cookies, headers=api_client.headers, json=json) attach_http(response) assert response.status_code == 400
def test_added_users(self, username, email, password, api_client: ApiClient): """Tests that users blocked Steps: - Make request to application with path /api/add_user and JSON in valid format - Make request to application with path /api/block_user/<username> - Make request to application with path /api/block_user/<username> again Expected results: Status code for the first request is in 200-300 range Status code for the second request is 200 Status code for the third request is 304 """ response = api_client.add_user(username=username, email=email, password=password) attach_http(response, '1') if response.status_code not in range(200, 300): raise Exception('there was error with add_user function') with allure.step('Block user'): response2 = api_client.block_user(username) attach_http(response2, '2') assert response2.status_code == status_codes.OK with allure.step('Block user again'): response3 = api_client.block_user(username) attach_http(response3, '3') assert response3.status_code == status_codes.NOT_CHANGED
def check_all_sources_exist(page: BasePage, page_name, msg=''): urls = get_sources_from_page(page) cookies = {'session': page.session_cookie} headers = {'User-Agent': page.user_agent} allure.attach(str(urls), name=f'sources_of_{page_name}.txt', attachment_type=allure.attachment_type.TEXT) all_right = True for url in urls: try: with allure.step(f'Check url {url}'): # allure step to check single url response = requests.get(change_netloc( url, page.settings.app_api_netloc), cookies=cookies, headers=headers, timeout=1) attach_http(response, url) assert response.status_code == 200 except AssertionError: all_right = False if not all_right: raise AssertionError(msg)
def test_scenario2(api_client: ApiClient): """Test scenario with several requests Steps: 0 Generate credentials 1 Delete user via del_user API function (user not exists) 2 Block user via block_user API function (user not exists) 3 Accept user via accept_user API function (user not exists) 4 Add user via add_user API function with credentials 5 Accept user via accept_user API function 6 Block user via block_user API function 7 Block user via block_user API function 8 Accept user via accept_user API function 9 Accept user via accept_user API function 0 Delete user via del_user API function 1 Add user via add_user API function with the same credentials 2 Add user via add_user API function 3 Add user via add_user API function with different email 4 Add user via add_user API function with different username Expected results: Steps 1-3 fail, status code 404 Step 4 succeed, status code 201 Step 5 status code 304 Step 6 status code 200 Step 7 status code 304 Step 8 status code 200 Step 9 status code 304 Step 10 status code 204 Step 11 status code 201 Step 12 status code 304 Step 13 status code 304 Step 14 status code 304 """ username = '******' email = '*****@*****.**' password = '******' with allure.step(f"Del user {username} who doesn't exist"): response = api_client.del_user(username) attach_http(response) assert response.status_code == status_codes.NOT_EXIST with allure.step(f"Block user {username} who doesn't exist"): response = api_client.block_user(username) attach_http(response) assert response.status_code == status_codes.NOT_EXIST with allure.step(f"Accept user {username} who doesn't exist"): response = api_client.accept_user(username) attach_http(response) assert response.status_code == status_codes.NOT_EXIST with allure.step( f'Add user with credentials ({username}, {email}, {password})'): response = api_client.add_user(username=username, email=email, password=password) attach_http(response) assert response.status_code == status_codes.CREATED with allure.step(f"Accept user {username}"): response = api_client.accept_user(username) attach_http(response) assert response.status_code == status_codes.NOT_CHANGED with allure.step(f"Block user {username}"): response = api_client.block_user(username) attach_http(response) assert response.status_code == status_codes.OK with allure.step(f"Block user {username} again"): response = api_client.block_user(username) attach_http(response) assert response.status_code == status_codes.NOT_CHANGED with allure.step(f"Accept user {username}"): response = api_client.accept_user(username) attach_http(response) assert response.status_code == status_codes.OK with allure.step(f"Accept user {username}"): response = api_client.accept_user(username) attach_http(response) assert response.status_code == status_codes.NOT_CHANGED with allure.step(f"Del user {username}"): response = api_client.del_user(username) attach_http(response) assert response.status_code == status_codes.DELETED with allure.step( f'Add user with credentials ({username}, {email}, {password})'): response = api_client.add_user(username=username, email=email, password=password) attach_http(response) assert response.status_code == status_codes.CREATED with allure.step( f'Try to add user with credentials ({username}, {email}, {password})' ): response = api_client.add_user(username=username, email=email, password=password) attach_http(response) assert response.status_code == status_codes.NOT_CHANGED with allure.step( f'Try to add user with credentials ({username}, {email}, {password})' ): response = api_client.add_user(username=username, email='a' + email, password=password) attach_http(response) assert response.status_code == status_codes.NOT_CHANGED with allure.step( f'Try to add user with credentials ({username}, {email}, {password})' ): response = api_client.add_user(username='******' + username, email=email, password=password) attach_http(response) assert response.status_code == status_codes.NOT_CHANGED
def test_scenario1(registration_page: RegistrationPage, api_client: ApiClient): """Test how API functions influence user Steps: 1 Pass registration with generated credentials 2 Block user via block_user API function 3 Refresh page 4 Open registration page and try to register with the same credentials again 5 Open authorization page and try to authorize to the site with the same credentials 6 Accept user via accept_user API function 7 Refresh page 8 Delete user via del_user API function 9 Refresh page 0 Open authorization page and try to authorize to the site with the same credentials 1 Open registration page and register with same credentials again Expected results: Steps 1-2 succeed Step 3 redirect to authrization page Steps 4-5 fail, pytest.raises catch Exception Step 6 succeed Step 7 redirect to main page Step 8 succeed Step 9 redirect to authorization page Step 10 fail, pytest.raises catch Exception Step 11 succeed """ username, email, password = make.auth_data() with allure.step("Pass registration"): main_page = registration_page.pass_registration(username=username, password=password, email=email) assert 'welcome' in urlparse(main_page.current_url).path with allure.step(f"Block user {username}"): response = api_client.block_user(username) attach_http(response) with allure.step("Refresh page"): main_page.refresh() assert 'welcome' not in urlparse(main_page.current_url).path with allure.step( "Open registration page and try to register with same credentials again" ): registration_page.make_request() with pytest.raises(RegistrationError): registration_page.pass_registration(username=username, email=email, password=password) with allure.step("Try to authorize"): authorization_page = registration_page.go_to_authorization_page() with pytest.raises(AuthorizationError): authorization_page.authorize(username=username, password=password) with allure.step("Accept user"): response = api_client.accept_user(username) attach_http(response) with allure.step('User was redirected to main page after refresh'): authorization_page.refresh() assert 'welcome' in urlparse(authorization_page.current_url).path with allure.step("Delete user"): api_client.del_user(username) attach_http(response) with allure.step("User was redirected from main page after refresh"): main_page.refresh() assert 'welcome' not in urlparse(authorization_page.current_url).path with allure.step("Try to authorize"): authorization_page.make_request() with pytest.raises(AuthorizationError): authorization_page.authorize(username=username, password=password) with allure.step("Pass registration"): registration_page.make_request() registration_page.pass_registration(username=username, email=email, password=password)