Skip to content

Commit bfad48d

Browse files
committed
Added Api tests for User and Books
1 parent 63e4f92 commit bfad48d

File tree

6 files changed

+123
-13
lines changed

6 files changed

+123
-13
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Net.Http.Json;
2+
using SeleniumTests.lib.config;
3+
using SeleniumTests.lib.models;
4+
5+
namespace SeleniumTests.lib.helpers;
6+
7+
public class BookStoreClient
8+
{
9+
private readonly HttpClient _client = new();
10+
11+
public BookStoreClient()
12+
{
13+
_client.BaseAddress = new Uri(Config.ApiUrl);
14+
}
15+
16+
public async Task<BooksResponseDto> GetAllBooksAsync()
17+
{
18+
return await _client.GetFromJsonAsync<BooksResponseDto>("/BookStore/v1/Books")
19+
?? new BooksResponseDto();
20+
}
21+
22+
public async Task<BookDto> GetBookByIsbnAsync(string isbn)
23+
{
24+
return await _client.GetFromJsonAsync<BookDto>($"/BookStore/v1/Book?ISBN={isbn}")
25+
?? new BookDto();
26+
}
27+
28+
public async Task<HttpResponseMessage> CreateUserAsync(UserRequestDto user)
29+
{
30+
return await _client.PostAsJsonAsync("/Account/v1/User", user);
31+
}
32+
33+
public async Task<TokenResponseDto?> GenerateTokenAsync(UserRequestDto user)
34+
{
35+
var res = await _client.PostAsJsonAsync("/Account/v1/GenerateToken", user);
36+
return await res.Content.ReadFromJsonAsync<TokenResponseDto>();
37+
}
38+
}

SeleniumTests/lib/models/BookDto.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ namespace SeleniumTests.lib.models;
22

33
public class BookDto
44
{
5-
public string isbn { get; set; }
6-
public string title { get; set; }
7-
public string subTitle { get; set; }
8-
public string author { get; set; }
9-
public string publish_date { get; set; }
10-
public string publisher { get; set; }
11-
public int pages { get; set; }
12-
public string description { get; set; }
13-
public string website { get; set; }
5+
public string ISBN { get; set; } = string.Empty;
6+
public string Title { get; set; } = string.Empty;
7+
public string SubTitle { get; set; } = string.Empty;
8+
public string Author { get; set; } = string.Empty;
9+
public string Publisher { get; set; } = string.Empty;
10+
public int Pages { get; set; }
11+
public string Description { get; set; } = string.Empty;
12+
public string Website { get; set; } = string.Empty;
1413
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace SeleniumTests.lib.models;
2+
3+
public class BooksResponseDto
4+
{
5+
public List<BookDto> Books { get; set; } = [];
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SeleniumTests.lib.models;
2+
3+
public class TokenResponseDto
4+
{
5+
public string Token { get; set; } = string.Empty;
6+
public string Expires { get; set; } = string.Empty;
7+
public string Status { get; set; } = string.Empty;
8+
public string Result { get; set; } = string.Empty;
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SeleniumTests.lib.models;
2+
3+
public class UserRequestDto
4+
{
5+
public string UserName { get; set; } = string.Empty;
6+
public string Password { get; set; } = string.Empty;
7+
}
Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,66 @@
11
using SeleniumTests.lib.helpers;
2+
using SeleniumTests.lib.models;
23

34
namespace SeleniumTests.tests;
45

6+
[TestFixture]
57
public class ApiBookStoreTests
68
{
9+
private BookStoreClient _client = null!;
10+
11+
[SetUp]
12+
public void Setup()
13+
{
14+
_client = new BookStoreClient();
15+
}
16+
17+
[Test]
18+
public async Task GetAllBooks_ShouldReturn_NonEmptyList()
19+
{
20+
var books = await _client.GetAllBooksAsync();
21+
Assert.That(books.Books, Is.Not.Empty);
22+
}
23+
24+
[Test]
25+
public async Task GetBookByIsbn_ShouldReturn_ValidBook()
26+
{
27+
var books = await _client.GetAllBooksAsync();
28+
var firstIsbn = books.Books.First().ISBN;
29+
30+
var book = await _client.GetBookByIsbnAsync(firstIsbn);
31+
32+
Assert.That(book.Title, Is.Not.Empty);
33+
Assert.That(book.ISBN, Is.EqualTo(firstIsbn));
34+
}
35+
36+
[Test]
37+
public async Task CreateUser_ShouldReturn_201()
38+
{
39+
var user = new UserRequestDto
40+
{
41+
UserName = $"user_{Guid.NewGuid():N}",
42+
Password = "P@ssword123"
43+
};
44+
45+
var response = await _client.CreateUserAsync(user);
46+
Assert.That((int)response.StatusCode, Is.EqualTo(201));
47+
}
48+
749
[Test]
8-
public async Task GetBooks_ShouldReturnBooksJson()
50+
public async Task GenerateToken_ShouldReturn_ValidToken()
951
{
10-
var api = new ApiClient();
11-
var books = await api.GetBooks();
52+
var user = new UserRequestDto
53+
{
54+
UserName = $"user_{Guid.NewGuid():N}",
55+
Password = "P@ssword123"
56+
};
57+
58+
await _client.CreateUserAsync(user);
59+
60+
var token = await _client.GenerateTokenAsync(user);
1261

13-
Assert.That(books, Is.Not.Null);
62+
Assert.That(token, Is.Not.Null);
63+
Assert.That(token!.Token, Is.Not.Empty);
64+
Assert.That(token.Status, Is.EqualTo("Success"));
1465
}
1566
}

0 commit comments

Comments
 (0)