|
1 | 1 | """Create Book Test Module""" |
| 2 | + |
| 3 | +import allure |
2 | 4 | import pytest |
3 | | -from tests.BaseTest import BaseTest |
| 5 | + |
4 | 6 | from helpers import validator |
| 7 | +from tests.base_test import BaseTest |
5 | 8 |
|
6 | 9 |
|
| 10 | +@allure.epic("Book Management") |
| 11 | +@allure.feature("Create Book") |
| 12 | +@allure.severity(allure.severity_level.CRITICAL) |
7 | 13 | class TestCreateBook(BaseTest): |
8 | | - """Creat Book Test Class""" |
| 14 | + """Create Book Test Class""" |
| 15 | + |
9 | 16 | HEADERS = {"authorization": "Bearer user-token"} |
10 | 17 |
|
11 | | - def create_and_validate_book(self, book, expected_status=201, expected_message=None): |
| 18 | + def create_and_validate_book( |
| 19 | + self, book, expected_status=201, expected_message=None |
| 20 | + ): |
12 | 21 | """Helper to create a book and validate the response.""" |
13 | 22 | response = self.client.post( |
14 | | - self.client.build_url(), json=book, headers=self.HEADERS) |
| 23 | + self.client.build_url(), json=book, headers=self.HEADERS |
| 24 | + ) |
15 | 25 | validator.validate_status_code(response, expected_status) |
16 | 26 | if expected_status == 201: |
17 | 27 | validator.validate_response_book(response, book) |
18 | 28 | elif expected_message: |
19 | 29 | validator.validate_error_message(response, expected_message) |
20 | 30 | return response |
21 | 31 |
|
| 32 | + @allure.title("Should create book when title and author are valid") |
22 | 33 | def test_should_create_book_when_title_and_author_are_valid(self): |
23 | 34 | """Test creating a book with valid title and author.""" |
24 | | - book = {"title": "New Book POST API Title", |
25 | | - "author": "New Book POST API Author"} |
| 35 | + book = { |
| 36 | + "title": "New Book POST API Title", |
| 37 | + "author": "New Book POST API Author", |
| 38 | + } |
26 | 39 | self.create_and_validate_book(book) |
27 | 40 |
|
| 41 | + @allure.title("Should reject duplicate book creation") |
28 | 42 | def test_should_reject_duplicate_book_creation(self): |
29 | 43 | """Test duplicate book creation returns 409.""" |
30 | | - book = {"title": "New Book POST API Title", |
31 | | - "author": "New Book POST API Author"} |
| 44 | + book = { |
| 45 | + "title": "New Book POST API Title", |
| 46 | + "author": "New Book POST API Author", |
| 47 | + } |
32 | 48 | self.create_and_validate_book( |
33 | | - book, expected_status=409, expected_message="A book with the same title and author already exists") |
| 49 | + book, |
| 50 | + expected_status=409, |
| 51 | + expected_message="A book with the same title and author already exists", |
| 52 | + ) |
34 | 53 |
|
| 54 | + @allure.title("Should create book when title is different for same author") |
35 | 55 | def test_should_create_book_when_title_is_different_for_same_author(self): |
36 | 56 | """Test creating a book with a different title for the same author.""" |
37 | | - book = {"title": "New Book POST API Title Different", |
38 | | - "author": "New Book POST API Author"} |
| 57 | + book = { |
| 58 | + "title": "New Book POST API Title Different", |
| 59 | + "author": "New Book POST API Author", |
| 60 | + } |
39 | 61 | self.create_and_validate_book(book) |
40 | 62 |
|
| 63 | + @allure.title("Should create book when author is different for same book") |
41 | 64 | def test_should_create_book_when_author_is_different_for_same_book(self): |
42 | 65 | """Test creating a book with a different author for the same title.""" |
43 | | - book = {"title": "New Book POST API Title Different", |
44 | | - "author": "New Book POST API Author Different"} |
| 66 | + book = { |
| 67 | + "title": "New Book POST API Title Different", |
| 68 | + "author": "New Book POST API Author Different", |
| 69 | + } |
45 | 70 | self.create_and_validate_book(book) |
46 | 71 |
|
| 72 | + @allure.title("Should return 401 when no auth token provided") |
47 | 73 | def test_should_return_401_when_no_auth_token_provided(self): |
48 | 74 | """Test creating a book without authentication returns 401.""" |
49 | | - book = {"title": "Error 401 Test Title", |
50 | | - "author": "Error 401 Test Author"} |
| 75 | + book = {"title": "Error 401 Test Title", "author": "Error 401 Test Author"} |
51 | 76 | response = self.client.post(self.client.build_url(), json=book) |
52 | 77 | validator.validate_status_code(response, 401) |
53 | | - validator.validate_error_message( |
54 | | - response, "Unauthorized. No token provided.") |
| 78 | + validator.validate_error_message(response, "Unauthorized. No token provided.") |
55 | 79 |
|
56 | | - @pytest.mark.parametrize("payload,expected_message", [ |
57 | | - ({}, "Both title and author are required."), |
58 | | - ({"author": "author only test"}, "Both title and author are required."), |
59 | | - ({"title": "title only test"}, "Both title and author are required."), |
60 | | - ]) |
| 80 | + @pytest.mark.parametrize( |
| 81 | + "payload,expected_message", |
| 82 | + [ |
| 83 | + ({}, "Both title and author are required."), |
| 84 | + ({"author": "author only test"}, "Both title and author are required."), |
| 85 | + ({"title": "title only test"}, "Both title and author are required."), |
| 86 | + ], |
| 87 | + ) |
| 88 | + @allure.title("Should reject book with missing fields {expected_message}") |
61 | 89 | def test_should_reject_book_with_missing_fields(self, payload, expected_message): |
62 | 90 | """Test creating a book with missing fields returns 400.""" |
63 | 91 | self.create_and_validate_book( |
64 | | - payload, expected_status=400, expected_message=expected_message) |
| 92 | + payload, expected_status=400, expected_message=expected_message |
| 93 | + ) |
65 | 94 |
|
| 95 | + @allure.title("Should reject book creation with client-provided ID") |
66 | 96 | def test_should_reject_book_creation_with_client_provided_id(self): |
67 | 97 | """Test creating a book with a client-provided ID returns 400.""" |
68 | | - book = {"id": "122233", "title": "Client provided Book ID Title", |
69 | | - "author": "Client provided Book Author"} |
| 98 | + book = { |
| 99 | + "id": "122233", |
| 100 | + "title": "Client provided Book ID Title", |
| 101 | + "author": "Client provided Book Author", |
| 102 | + } |
70 | 103 | self.create_and_validate_book( |
71 | | - book, expected_status=400, expected_message="ID must not be provided when creating a book") |
| 104 | + book, |
| 105 | + expected_status=400, |
| 106 | + expected_message="ID must not be provided when creating a book", |
| 107 | + ) |
0 commit comments