1
1
using FluentAssertions ;
2
+ using FluentAssertions . Specialized ;
3
+ using JsonApiDotNetCore . OpenApi . Client ;
4
+ using Newtonsoft . Json ;
2
5
using OpenApiEndToEndTests . ClientGeneratedId . GeneratedCode ;
3
6
using OpenApiTests ;
4
7
using OpenApiTests . ClientGeneratedId ;
@@ -18,10 +21,11 @@ public PostTests(IntegrationTestContext<OpenApiStartup<ClientGeneratedIdDbContex
18
21
19
22
testContext . UseController < PlayersController > ( ) ;
20
23
testContext . UseController < GamesController > ( ) ;
24
+ testContext . UseController < GroupsController > ( ) ;
21
25
}
22
26
23
27
[ Fact ]
24
- public async Task Returns_error_if_required_id_is_omitted ( )
28
+ public async Task Omit_required_id ( )
25
29
{
26
30
// Arrange
27
31
Player player = _fakers . Player . Generate ( ) ;
@@ -30,35 +34,35 @@ public async Task Returns_error_if_required_id_is_omitted()
30
34
ClientGeneratedIdClient apiClient = new ( httpClient ) ;
31
35
32
36
// Act
33
- Func < Task < PlayerPrimaryResponseDocument > > action = ( ) => apiClient . PostPlayerAsync ( null , new PlayerPostRequestDocument
37
+ Func < Task < PlayerPrimaryResponseDocument ? > > action = ( ) => ApiResponse . TranslateAsync ( ( ) => apiClient . PostPlayerAsync ( null , new PlayerPostRequestDocument
34
38
{
35
39
Data = new PlayerDataInPostRequest
36
40
{
37
- Id = null ! , // FIXME: passing "" here works fine 🤔
41
+ Id = null ! ,
38
42
Attributes = new PlayerAttributesInPostRequest
39
43
{
40
44
Name = player . Name
41
45
}
42
46
}
43
- } ) ;
47
+ } ) ) ;
44
48
45
49
// Assert
46
- var exception = ( await action . Should ( ) . ThrowAsync < Exception > ( ) ) . Subject . First ( ) ;
47
- // Exception is Newtonsoft.Json.JsonSerializationException: Cannot write a null value for property 'id'. Property requires a value. Path 'data'.
48
- // Probably not what we want.
50
+ ExceptionAssertions < JsonSerializationException > assertion = await action . Should ( ) . ThrowExactlyAsync < JsonSerializationException > ( ) ;
51
+ assertion . Which . Message . Should ( ) . Be ( "Cannot write a null value for property 'id'. Property requires a value. Path 'data'." ) ;
49
52
}
50
53
51
54
[ Fact ]
52
- public async Task Requires_passing_id ( )
55
+ public async Task Pass_required_id ( )
53
56
{
54
57
// Arrange
55
58
Player player = _fakers . Player . Generate ( ) ;
59
+ player . Id = Guid . NewGuid ( ) ;
56
60
57
61
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
58
62
ClientGeneratedIdClient apiClient = new ( httpClient ) ;
59
63
60
64
// Act
61
- Func < Task < PlayerPrimaryResponseDocument > > action = ( ) => apiClient . PostPlayerAsync ( null , new PlayerPostRequestDocument
65
+ Func < Task < PlayerPrimaryResponseDocument ? > > action = ( ) => ApiResponse . TranslateAsync ( ( ) => apiClient . PostPlayerAsync ( null , new PlayerPostRequestDocument
62
66
{
63
67
Data = new PlayerDataInPostRequest
64
68
{
@@ -68,15 +72,15 @@ public async Task Requires_passing_id()
68
72
Name = player . Name
69
73
}
70
74
}
71
- } ) ;
75
+ } ) ) ;
72
76
73
77
// Assert
74
- PlayerPrimaryResponseDocument doc = ( await action . Should ( ) . NotThrowAsync ( ) ) . Subject ;
75
- doc . Data . Id . Should ( ) . Be ( player . StringId ) ;
78
+ PlayerPrimaryResponseDocument ? doc = ( await action . Should ( ) . NotThrowAsync ( ) ) . Subject ;
79
+ doc . Should ( ) . BeNull ( ) ;
76
80
}
77
81
78
82
[ Fact ]
79
- public async Task Allows_passing_id ( )
83
+ public async Task Omit_allowed_id ( )
80
84
{
81
85
// Arrange
82
86
Game game = _fakers . Game . Generate ( ) ;
@@ -85,49 +89,76 @@ public async Task Allows_passing_id()
85
89
ClientGeneratedIdClient apiClient = new ( httpClient ) ;
86
90
87
91
// Act
88
- Func < Task < GamePrimaryResponseDocument > > action = ( ) => apiClient . PostGameAsync ( null , new GamePostRequestDocument
92
+ Func < Task < GamePrimaryResponseDocument ? > > action = ( ) => ApiResponse . TranslateAsync ( ( ) => apiClient . PostGameAsync ( null , new GamePostRequestDocument
89
93
{
90
94
Data = new GameDataInPostRequest
91
95
{
92
- Id = game . StringId ! , // FIXME: StringId is null, how to generate an id?
96
+ Id = null ! ,
93
97
Attributes = new GameAttributesInPostRequest
94
98
{
95
99
Name = game . Name ,
96
100
Price = ( double ) game . Price
97
101
}
98
102
}
99
- } ) ;
103
+ } ) ) ;
100
104
101
105
// Assert
102
- GamePrimaryResponseDocument doc = ( await action . Should ( ) . NotThrowAsync ( ) ) . Subject ;
103
- doc . Data . Id . Should ( ) . Be ( game . StringId ) ;
106
+ GamePrimaryResponseDocument ? doc = ( await action . Should ( ) . NotThrowAsync ( ) ) . Subject ;
107
+ doc ? . Data . Id . Should ( ) . NotBeNullOrEmpty ( ) ;
104
108
}
105
109
106
110
[ Fact ]
107
- public async Task Allow_omitting_id ( )
111
+ public async Task Pass_allowed_id ( )
108
112
{
109
113
// Arrange
110
114
Game game = _fakers . Game . Generate ( ) ;
115
+ game . Id = Guid . NewGuid ( ) ;
111
116
112
117
using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
113
118
ClientGeneratedIdClient apiClient = new ( httpClient ) ;
114
119
115
120
// Act
116
- Func < Task < GamePrimaryResponseDocument > > action = ( ) => apiClient . PostGameAsync ( null , new GamePostRequestDocument
121
+ Func < Task < GamePrimaryResponseDocument ? > > action = ( ) => ApiResponse . TranslateAsync ( ( ) => apiClient . PostGameAsync ( null , new GamePostRequestDocument
117
122
{
118
123
Data = new GameDataInPostRequest
119
124
{
120
- Id = null ! , // FIXME: incorrect nullability here
125
+ Id = game . StringId ! ,
121
126
Attributes = new GameAttributesInPostRequest
122
127
{
123
128
Name = game . Name ,
124
129
Price = ( double ) game . Price
125
130
}
126
131
}
127
- } ) ;
132
+ } ) ) ;
128
133
129
134
// Assert
130
- GamePrimaryResponseDocument doc = ( await action . Should ( ) . NotThrowAsync ( ) ) . Subject ;
131
- doc . Data . Id . Should ( ) . NotBeNullOrEmpty ( ) ;
135
+ GamePrimaryResponseDocument ? doc = ( await action . Should ( ) . NotThrowAsync ( ) ) . Subject ;
136
+ doc . Should ( ) . BeNull ( ) ;
132
137
}
133
- }
138
+
139
+ [ Fact ]
140
+ public async Task Omit_forbidden_id ( )
141
+ {
142
+ // Arrange
143
+ Group group = _fakers . Group . Generate ( ) ;
144
+
145
+ using HttpClient httpClient = _testContext . Factory . CreateClient ( ) ;
146
+ ClientGeneratedIdClient apiClient = new ( httpClient ) ;
147
+
148
+ // Act
149
+ Func < Task < GroupPrimaryResponseDocument ? > > action = ( ) => ApiResponse . TranslateAsync ( ( ) => apiClient . PostGroupAsync ( null , new GroupPostRequestDocument
150
+ {
151
+ Data = new GroupDataInPostRequest
152
+ {
153
+ Attributes = new GroupAttributesInPostRequest
154
+ {
155
+ Name = group . Name
156
+ }
157
+ }
158
+ } ) ) ;
159
+
160
+ // Assert
161
+ GroupPrimaryResponseDocument ? doc = ( await action . Should ( ) . NotThrowAsync ( ) ) . Subject ;
162
+ doc ? . Data . Id . Should ( ) . NotBeNullOrEmpty ( ) ;
163
+ }
164
+ }
0 commit comments