forked from LittleBigRefresh/Refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserApiTests.cs
More file actions
162 lines (129 loc) · 6.37 KB
/
Copy pathUserApiTests.cs
File metadata and controls
162 lines (129 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using Refresh.Database.Models.Authentication;
using Refresh.GameServer.Endpoints.ApiV3.ApiTypes;
using Refresh.GameServer.Endpoints.ApiV3.ApiTypes.Errors;
using Refresh.GameServer.Endpoints.ApiV3.DataTypes.Request.Authentication;
using Refresh.GameServer.Endpoints.ApiV3.DataTypes.Response.Users;
using Refresh.Database.Models.Users;
using RefreshTests.GameServer.Extensions;
namespace RefreshTests.GameServer.Tests.ApiV3;
public class UserApiTests : GameServerTest
{
[Test]
public void GetsUserByUsername()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();
ApiResponse<ApiGameUserResponse>? response = context.Http.GetData<ApiGameUserResponse>($"/api/v3/users/name/{user.Username}");
Assert.That(response, Is.Not.Null);
response = context.Http.GetData<ApiGameUserResponse>($"/api/v3/users/uuid/{user.Username}");
Assert.That(response, Is.Not.Null);
response!.AssertErrorIsEqual(ApiNotFoundError.UserMissingError);
}
[Test]
public void RegisterAccount()
{
using TestContext context = this.GetServer();
const string username = "a_lil_guy";
ApiResponse<ApiAuthenticationResponse>? response = context.Http.PostData<ApiAuthenticationResponse>("/api/v3/register", new ApiRegisterRequest
{
Username = username,
EmailAddress = "guy@lil.com",
PasswordSha512 = "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff",
});
Assert.That(response, Is.Not.EqualTo(null));
context.Database.Refresh();
Assert.That(context.Database.GetUserByUsername(username), Is.Not.EqualTo(null));
}
[Test]
public void CannotRegisterAccountWithDisallowedUsername()
{
using TestContext context = this.GetServer();
const string username = "a_lil_guy";
context.Database.DisallowUser(username);
ApiResponse<ApiAuthenticationResponse>? response = context.Http.PostData<ApiAuthenticationResponse>("/api/v3/register", new ApiRegisterRequest
{
Username = username,
EmailAddress = "guy@lil.com",
PasswordSha512 = "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff",
});
Assert.That(response, Is.Not.Null);
Assert.That(response!.Error, Is.Not.EqualTo(null));
Assert.That(response.Error!.Name, Is.EqualTo("ApiAuthenticationError"));
context.Database.Refresh();
Assert.That(context.Database.GetUserByUsername(username), Is.EqualTo(null));
}
[TestCase("4")]
[TestCase("44444444444444444444444444444444444444")]
[TestCase("$#*(72($&8#$")]
public void CannotRegisterAccountWithInvalidUsername(string username)
{
using TestContext context = this.GetServer();
ApiResponse<ApiAuthenticationResponse>? response = context.Http.PostData<ApiAuthenticationResponse>("/api/v3/register", new ApiRegisterRequest
{
Username = username,
EmailAddress = "far4@toolkit.vita",
PasswordSha512 = "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff",
});
Assert.That(response, Is.Not.Null);
Assert.That(response!.Error, Is.Not.Null);
Assert.That(response.Error!.Name, Is.EqualTo("ApiValidationError"));
context.Database.Refresh();
Assert.That(context.Database.GetUserByUsername(username), Is.EqualTo(null));
}
[Test]
public void GetsUserByUuid()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();
ApiResponse<ApiGameUserResponse>? response = context.Http.GetData<ApiGameUserResponse>($"/api/v3/users/uuid/{user.UserId.ToString()}");
Assert.That(response, Is.Not.Null);
response = context.Http.GetData<ApiGameUserResponse>($"/api/v3/users/name/{user.UserId.ToString()}");
Assert.That(response, Is.Not.Null);
response!.AssertErrorIsEqual(ApiNotFoundError.UserMissingError);
}
[Test]
public void UserNotFound()
{
using TestContext context = this.GetServer();
context.CreateUser();
ApiResponse<ApiGameUserResponse>? response = context.Http.GetData<ApiGameUserResponse>("/api/v3/users/name/dingus");
Assert.That(response, Is.Not.Null);
response!.AssertErrorIsEqual(ApiNotFoundError.UserMissingError);
response = context.Http.GetData<ApiGameUserResponse>("/api/v3/users/uuid/dingus");
Assert.That(response, Is.Not.Null);
response!.AssertErrorIsEqual(ApiNotFoundError.UserMissingError);
}
[Test]
public void DoesntGetOwnUserWhenUnauthed()
{
using TestContext context = this.GetServer();
context.CreateUser();
HttpResponseMessage response = context.Http.GetAsync("/api/v3/users/me").Result;
Assert.That(response.StatusCode, Is.EqualTo(Forbidden));
}
[Test]
public void GetsOwnUserWhenAuthed()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();
using HttpClient client = context.GetAuthenticatedClient(TokenType.Api, user);
ApiResponse<ApiGameUserResponse>? response = client.GetData<ApiGameUserResponse>("/api/v3/users/me");
Assert.That(response, Is.Not.Null);
Assert.That(response!.Data!.Username, Is.EqualTo(user.Username));
}
[Test]
public void CanPatchOwnUser()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();
const string description = "yeah";
using HttpClient client = context.GetAuthenticatedClient(TokenType.Api, user);
Assert.That(user.Description, Is.EqualTo(string.Empty));
object payload = new { description };
ApiResponse<ApiGameUserResponse>? response = client.PatchData<ApiGameUserResponse>("/api/v3/users/me", payload);
Assert.That(response, Is.Not.Null);
Assert.That(response!.Data!.Description, Is.EqualTo(description));
context.Database.Refresh();
Assert.That(user.Description, Is.EqualTo(description));
}
}