Skip to content

Commit

Permalink
#18: Synergy.Web.Api.Testing: Sample extended
Browse files Browse the repository at this point in the history
Added DateTime serialization
  • Loading branch information
MarcinCelej committed May 13, 2024
1 parent 9c7eb0c commit 21e9ca0
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
namespace Synergy.Sample.Web.API.Services.Users.Domain
using System;

namespace Synergy.Sample.Web.API.Services.Users.Domain
{
public class User
{
public string Id { get; }
public Login Login { get; }
public DateTime CreatedOn { get; }

public User(string id, Login login)
{
this.Id = id;
this.Login = login;
this.CreatedOn = DateTime.Now;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using Newtonsoft.Json;
using System;
using Newtonsoft.Json;
using Synergy.Sample.Web.API.Services.Users.Domain;

namespace Synergy.Sample.Web.API.Services.Users
{
public class UserReadModel
{
[JsonProperty("id")]
public string Id { get; }

[JsonProperty("login")]
public Login Login { get; }
[JsonProperty("id")] public string Id { get; }
[JsonProperty("login")] public Login Login { get; }
[JsonProperty("created")] public DateTime CreatedOn { get; }

public UserReadModel(string id, Login login)
{
Expand All @@ -21,6 +20,7 @@ public UserReadModel(User user)
{
this.Id = user.Id;
this.Login = user.Login;
this.CreatedOn = user.CreatedOn;
}
}
}
39 changes: 22 additions & 17 deletions Web/Sample/Synergy.Sample.Web.API.Tests/Contracts/swagger-v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@
},
"paths": {
"/api/v1/users": {
"get": {
"tags": [
"Users"
],
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GetUsersQueryResult"
}
}
}
}
}
},
"post": {
"tags": [
"Users"
Expand Down Expand Up @@ -83,6 +66,23 @@
}
}
}
},
"get": {
"tags": [
"Users"
],
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GetUsersQueryResult"
}
}
}
}
}
}
},
"/api/v1/users/{userId}": {
Expand Down Expand Up @@ -238,6 +238,11 @@
},
"login": {
"$ref": "#/components/schemas/Login"
},
"createdOn": {
"type": "string",
"format": "date-time",
"readOnly": true
}
},
"additionalProperties": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
"response": {
"status": "201 Created",
"headers": {
"Location": "http://localhost/api/v1/users/102a023150d94b1b99dbfbd5d6a695d8",
"Location": "http://localhost/api/v1/users/0dd576b44c7a40a4884cc7512272e751",
"api-supported-versions": "1.0",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "129"
"Content-Length": "182"
},
"body": {
"user": {
"id": "102a023150d94b1b99dbfbd5d6a695d8",
"id": "0dd576b44c7a40a4884cc7512272e751",
"login": {
"value": "[email protected]"
}
},
"created": "2024-05-13T13:19:15.8420304+02:00"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"Manual: User details are returned"
],
"request": {
"description": "Get user located at http://localhost/api/v1/users/102a023150d94b1b99dbfbd5d6a695d8",
"method": "GET /api/v1/users/102a023150d94b1b99dbfbd5d6a695d8",
"description": "Get user located at http://localhost/api/v1/users/0dd576b44c7a40a4884cc7512272e751",
"method": "GET /api/v1/users/0dd576b44c7a40a4884cc7512272e751",
"headers": {
"test": "header"
}
Expand All @@ -17,14 +17,15 @@
"headers": {
"api-supported-versions": "1.0",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "129"
"Content-Length": "182"
},
"body": {
"user": {
"id": "102a023150d94b1b99dbfbd5d6a695d8",
"id": "0dd576b44c7a40a4884cc7512272e751",
"login": {
"value": "[email protected]"
}
},
"created": "2024-05-13T13:19:15.8420304+02:00"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"Manual: User details are returned"
],
"request": {
"description": "Get user with id \"102a023150d94b1b99dbfbd5d6a695d8\"",
"method": "GET /api/v1/users/102a023150d94b1b99dbfbd5d6a695d8",
"description": "Get user with id \"0dd576b44c7a40a4884cc7512272e751\"",
"method": "GET /api/v1/users/0dd576b44c7a40a4884cc7512272e751",
"headers": {
"test": "header"
}
Expand All @@ -17,14 +17,15 @@
"headers": {
"api-supported-versions": "1.0",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "129"
"Content-Length": "182"
},
"body": {
"user": {
"id": "102a023150d94b1b99dbfbd5d6a695d8",
"id": "0dd576b44c7a40a4884cc7512272e751",
"login": {
"value": "[email protected]"
}
},
"created": "2024-05-13T13:19:15.8420304+02:00"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
"headers": {
"api-supported-versions": "1.0",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "152"
"Content-Length": "207"
},
"body": {
"users": [
{
"id": "102a023150d94b1b99dbfbd5d6a695d8",
"id": "0dd576b44c7a40a4884cc7512272e751",
"login": {
"value": "[email protected]"
}
},
"created": "2024-05-13T13:19:15.8420304+02:00"
}
]
}
Expand Down
8 changes: 4 additions & 4 deletions Web/Sample/Synergy.Sample.Web.API.Tests/Users/UserScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private string CreateUser()
this.EqualToPattern("/Patterns/S02_E01_CreateUser.json")
.Ignore(Ignore.RequestBody("Login"))
.Ignore(Ignore.ResponseLocationHeader())
.Ignore(Ignore.ResponseBody("user.id"))
.Ignore(Ignore.ResponseBody("user.id", "user.created"))
.Expected("Manual: User is created and its details are returned"))
.ReadUserId(out var id)
.ReadCreatedUserLocationUrl(out var location);
Expand All @@ -81,7 +81,7 @@ private string CreateUser()
.ShouldBe(
this.EqualToPattern("/Patterns/S02_E02_GetCreatedUserByLocation.json")
.Ignore(Ignore.RequestMethod())
.Ignore(Ignore.ResponseBody("user.id"))
.Ignore(Ignore.ResponseBody("user.id", "user.created"))
.Expected("Manual: User details are returned"));

return id;
Expand All @@ -97,7 +97,7 @@ private void GetUser(string userId)
.ShouldBe(
this.EqualToPattern("/Patterns/S03_E01_GetUser.json")
.Ignore(Ignore.RequestMethod())
.Ignore(Ignore.ResponseBody("user.id"))
.Ignore(Ignore.ResponseBody("user.id", "user.created"))
.Expected("Manual: User details are returned"));

this.users.GetUser("user-id-that-do-not-exist")
Expand All @@ -119,7 +119,7 @@ private void GetListOfUsers()
.ShouldBe(ApiConventionFor.GetListOfResources())
.ShouldBe(
this.EqualToPattern("/Patterns/S04_E01_GetListOfUsers.json")
.Ignore(Ignore.ResponseBody("users[*].id"))
.Ignore(Ignore.ResponseBody("users[*].id", "users[*].created"))
.Expected("Manual: Users list is returned")
);
}
Expand Down
35 changes: 19 additions & 16 deletions Web/Sample/Synergy.Sample.Web.API.Tests/Users/Users.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ Content-Length: 129

### 2.2. Get created user pointed by "Location" header (1 request)

### 2.2.2. Request to [Get user located at http://localhost/api/v1/users/2fdc2f9a03534536b1b5532a383b7cc0]
### 2.2.2. Request to [Get created user]

- Request
```
GET /api/v1/users/102a023150d94b1b99dbfbd5d6a695d8
GET /api/v1/users/0dd576b44c7a40a4884cc7512272e751
test: header
```

Expand All @@ -108,13 +108,14 @@ test: header
HTTP/1.1 200 OK
api-supported-versions: 1.0
Content-Type: application/json; charset=utf-8
Content-Length: 129
Content-Length: 182
{
"user": {
"id": "102a023150d94b1b99dbfbd5d6a695d8",
"id": "0dd576b44c7a40a4884cc7512272e751",
"login": {
"value": "[email protected]"
}
},
"created": "2024-05-13T13:19:15.8420304+02:00"
}
}
```
Expand All @@ -137,11 +138,11 @@ Content-Length: 129

### 3.1. Get user by id (1 request)

### 3.1.1. Request to [Get user with id "2fdc2f9a03534536b1b5532a383b7cc0"]
### 3.1.1. Request to [Get user by id]

- Request
```
GET /api/v1/users/102a023150d94b1b99dbfbd5d6a695d8
GET /api/v1/users/0dd576b44c7a40a4884cc7512272e751
test: header
```

Expand All @@ -150,13 +151,14 @@ test: header
HTTP/1.1 200 OK
api-supported-versions: 1.0
Content-Type: application/json; charset=utf-8
Content-Length: 129
Content-Length: 182
{
"user": {
"id": "102a023150d94b1b99dbfbd5d6a695d8",
"id": "0dd576b44c7a40a4884cc7512272e751",
"login": {
"value": "[email protected]"
}
},
"created": "2024-05-13T13:19:15.8420304+02:00"
}
}
```
Expand All @@ -171,7 +173,7 @@ Content-Length: 129

### 3.2. Negative test: Try to get user that do not exist (1 request)

### 3.2.2. Request to [Get user with id "user-id-that-do-not-exist"]
### 3.2.2. Request to [Get user by id]

- Request
```
Expand Down Expand Up @@ -222,14 +224,15 @@ test: header
HTTP/1.1 200 OK
api-supported-versions: 1.0
Content-Type: application/json; charset=utf-8
Content-Length: 152
Content-Length: 207
{
"users": [
{
"id": "102a023150d94b1b99dbfbd5d6a695d8",
"id": "0dd576b44c7a40a4884cc7512272e751",
"login": {
"value": "[email protected]"
}
},
"created": "2024-05-13T13:19:15.8420304+02:00"
}
]
}
Expand Down Expand Up @@ -391,7 +394,7 @@ Content-Length: 351

### 6.1. Delete user by id (1 request)

### 6.1.1. Request to [Delete user with id "2fdc2f9a03534536b1b5532a383b7cc0"]
### 6.1.1. Request to [Delete user by id]

- Request
```
Expand All @@ -418,7 +421,7 @@ Content-Length: 2

### 6.2. Try to get the deleted user (1 request)

### 6.2.2. Request to [Get user with id "2fdc2f9a03534536b1b5532a383b7cc0"]
### 6.2.2. Request to [Get user by id]

- Request
```
Expand Down
6 changes: 3 additions & 3 deletions Web/Sample/Synergy.Sample.Web.API.Tests/Users/UsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public HttpOperation GetAll()

public HttpOperation GetUserBy(Uri location)
=> this._testServer.Get(location.ToString())
.Details($"Get user located at {location}");
.Details($"Get created user");

public HttpOperation GetUser(string userId)
=> this._testServer.Get($"{UsersClient.Path}/{userId}")
.Details($"Get user with id {userId.QuoteOrNull()}");
.Details($"Get user by id");

public CreateUserOperation Create(string login)
=> this._testServer.Post<CreateUserOperation>(UsersClient.Path, body: JObject.Parse($"{{login:{login.QuoteOrNull()}}}"))
Expand All @@ -39,7 +39,7 @@ public CreateUserOperation Create(Login login)

public HttpOperation DeleteUser(string userId)
=> this._testServer.Delete($"{UsersClient.Path}/{userId}")
.Details($"Delete user with id {userId.QuoteOrNull()}");
.Details($"Delete user by id");

public class CreateUserOperation : HttpOperation
{
Expand Down

0 comments on commit 21e9ca0

Please sign in to comment.