forked from LittleBigRefresh/Refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestContext.cs
More file actions
291 lines (248 loc) · 10.1 KB
/
Copy pathTestContext.cs
File metadata and controls
291 lines (248 loc) · 10.1 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using Bunkum.Core;
using Bunkum.Core.Services;
using Bunkum.Core.Storage;
using Bunkum.Protocols.Http.Direct;
using JetBrains.Annotations;
using Refresh.Core.Services;
using Refresh.Core.Types.Data;
using Refresh.Database.Models.Authentication;
using Refresh.Database;
using Refresh.Database.Models.Users;
using RefreshTests.GameServer.Time;
using Refresh.Database.Models.Levels.Scores;
using Refresh.Database.Models.Levels;
using Refresh.Interfaces.Game.Types.UserData.Leaderboard;
using Refresh.Workers;
using Refresh.Interfaces.Game.Endpoints.DataTypes.Request;
using Refresh.Database.Models.Playlists;
using Refresh.Interfaces.APIv3.Endpoints.DataTypes.Request;
using Refresh.Database.Models.Photos;
using Microsoft.EntityFrameworkCore;
namespace RefreshTests.GameServer;
public class TestContext : IDisposable
{
public Lazy<TestRefreshGameServer> Server { get; }
public GameDatabaseContext Database { get; }
public TestGameDatabaseProvider DatabaseProvider { get; }
public HttpClient Http { get; }
public MockDateTimeProvider Time { get; }
private DirectHttpListener Listener { get; }
public TestContext(Lazy<TestRefreshGameServer> server, TestGameDatabaseProvider database, HttpClient http, DirectHttpListener listener, MockDateTimeProvider time)
{
this.Server = server;
this.DatabaseProvider = database;
this.Database = database.GetContext();
this.Http = http;
this.Listener = listener;
this.Time = time;
}
private int _users = 100; // start at 100 since usernames require 3 characters
private int UserIncrement => this._users++;
public HttpClient GetAuthenticatedClient(TokenType type,
GameUser? user = null,
int tokenExpirySeconds = GameDatabaseContext.DefaultTokenExpirySeconds,
string? ipAddress = null)
{
return this.GetAuthenticatedClient(type, out _, user, tokenExpirySeconds, ipAddress);
}
public HttpClient GetAuthenticatedClient(TokenType type, TokenGame game, TokenPlatform platform,
GameUser? user = null,
int tokenExpirySeconds = GameDatabaseContext.DefaultTokenExpirySeconds,
string? ipAddress = null)
{
return this.GetAuthenticatedClient(type, game, platform, out string _, user, tokenExpirySeconds, ipAddress);
}
public HttpClient GetAuthenticatedClient(TokenType type, out string tokenData,
GameUser? user = null,
int tokenExpirySeconds = GameDatabaseContext.DefaultTokenExpirySeconds,
string? ipAddress = null)
{
user ??= this.CreateUser();
TokenGame game = type switch
{
TokenType.Game => TokenGame.LittleBigPlanet2,
_ => TokenGame.Website,
};
TokenPlatform platform = type switch
{
TokenType.Game => TokenPlatform.PS3,
_ => TokenPlatform.Website,
};
return this.GetAuthenticatedClient(type, game, platform, out tokenData, user, tokenExpirySeconds, ipAddress);
}
public HttpClient GetAuthenticatedClient(TokenType type, TokenGame game, TokenPlatform platform, out string tokenData,
GameUser? user = null,
int tokenExpirySeconds = GameDatabaseContext.DefaultTokenExpirySeconds,
string? ipAddress = null)
{
Token token = this.GenerateToken(user, type, game, platform, ipAddress, tokenExpirySeconds);
tokenData = token.TokenData;
return this.GetAuthenticatedClient(token.TokenData, type);
}
public HttpClient GetAuthenticatedClient(TokenType type, TokenGame game, TokenPlatform platform, out Token token,
GameUser? user = null,
int tokenExpirySeconds = GameDatabaseContext.DefaultTokenExpirySeconds,
string? ipAddress = null)
{
token = this.GenerateToken(user, type, game, platform, ipAddress, tokenExpirySeconds);
return this.GetAuthenticatedClient(token.TokenData, type);
}
public HttpClient GetAuthenticatedClient(string tokenData, TokenType type)
{
HttpClient client = this.Listener.GetClient();
if (type == TokenType.Game)
{
client.DefaultRequestHeaders.Add("Cookie", "MM_AUTH=" + tokenData);
}
else
{
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", tokenData);
}
return client;
}
public Token GenerateToken(GameUser? user, TokenType type, TokenGame game, TokenPlatform platform, string? ipAddress, int tokenExpirySeconds)
{
return this.Database.GenerateTokenForUser(user ?? this.CreateUser(), type, game, platform, ipAddress ?? "0.0.0.0", tokenExpirySeconds);
}
public GameUser CreateUser(string? username = null, GameUserRole role = GameUserRole.User, bool verifyEmail = true)
{
username ??= this.UserIncrement.ToString();
GameUser user = this.Database.CreateUser(username, $"{username}@{username}.local");
if (role != GameUserRole.User) this.Database.SetUserRole(user, role);
if (verifyEmail) this.Database.VerifyUserEmail(user);
this.Database.Entry(user).State = EntityState.Unchanged;
return user;
}
public Token CreateToken(GameUser user, TokenType type = TokenType.Game, TokenGame game = TokenGame.LittleBigPlanet2, TokenPlatform platform = TokenPlatform.PS3)
{
return this.Database.GenerateTokenForUser(user, type, game, platform, "0.0.0.0");
}
public GameLevel CreateLevel(GameUser author, string title = "Level", TokenGame gameVersion = TokenGame.LittleBigPlanet1, int minPlayers = 1, int maxPlayers = 4)
{
GameLevelRequest levelRequest = new()
{
Title = title,
RootResource = "0",
};
GameLevel level = this.Database.AddLevel(levelRequest, gameVersion, author);
return level;
}
public GameLevel CreateLevel(GameUser author, string title, string description, TokenGame gameVersion = TokenGame.LittleBigPlanet1)
{
GameLevelRequest levelRequest = new()
{
Title = title,
Description = description,
RootResource = "0",
};
GameLevel level = this.Database.AddLevel(levelRequest, gameVersion, author);
return level;
}
public GameLevel CreateLevelWithRootResource(GameUser author, string rootResource, TokenGame gameVersion = TokenGame.LittleBigPlanet1)
{
GameLevelRequest levelRequest = new()
{
RootResource = rootResource,
};
GameLevel level = this.Database.AddLevel(levelRequest, gameVersion, author);
return level;
}
public GamePhoto CreatePhotoWithSubject(GameUser author, string imageHash, GameLevel? level = null, List<SerializedPhotoSubject>? subjects = null)
{
// TODO: Return newly created GamePhoto
if (this.Database.GetAssetFromHash(imageHash) == null)
{
this.Database.AddAssetToDatabase(new()
{
AssetHash = imageHash,
});
}
SerializedPhoto photo = new()
{
AuthorName = author.Username,
SmallHash = imageHash,
MediumHash = imageHash,
LargeHash = imageHash,
};
subjects ??=
[
new()
{
Username = author.Username,
DisplayName = author.Username,
BoundsList = "10,10,10,10"
}
];
return this.Database.UploadPhoto(photo, subjects, author, level);
}
public void FillLeaderboard(GameLevel level, int count, byte type)
{
for (byte i = 0; i < count; i++)
{
string username = "score" + i;
GameUser scoreUser = this.Database.GetUserByUsername(username) ?? this.CreateUser(username);
this.SubmitScore(i, type, level, scoreUser, TokenGame.LittleBigPlanet2, TokenPlatform.PS3);
}
}
public GameScore SubmitScore(int score, byte type, GameLevel level, GameUser user, TokenGame game, TokenPlatform platform, IList<GameUser>? players = null)
{
SerializedScore scoreObject = new()
{
Host = true,
Score = score,
ScoreType = type,
};
players ??= [user];
GameScore submittedScore = this.Database.SubmitScore(scoreObject, user, level, game, platform, players);
Assert.That(submittedScore, Is.Not.Null);
return submittedScore;
}
public GamePlaylist CreatePlaylist(GameUser author, string? title = null)
{
ApiPlaylistCreationRequest playlist = new()
{
Name = title
};
return this.Database.CreatePlaylist(author, playlist);
}
[Pure]
public TService GetService<TService>() where TService : Service => this.Server.Value.GetService<TService>();
public DataContext GetDataContext(Token? token = null, IDataStore? dataStore = null)
{
return new DataContext
{
Database = this.Database,
Logger = this.Server.Value.Logger,
DataStore = dataStore ?? (IDataStore)this.GetService<StorageService>()
.AddParameterToEndpoint(null!, new BunkumParameterInfo(typeof(IDataStore), ""), null!)!,
Match = this.GetService<MatchService>(),
Token = token,
GuidChecker = this.GetService<GuidCheckerService>(),
Cache = this.GetService<CacheService>(),
};
}
public WorkContext GetWorkContext()
{
return new WorkContext
{
Database = this.Database,
Logger = this.Server.Value.Logger,
DataStore = this.GetDataStore(),
};
}
public IDataStore GetDataStore()
{
return (IDataStore)this.GetService<StorageService>()
.AddParameterToEndpoint(null!, new BunkumParameterInfo(typeof(IDataStore), ""), null!)!;
}
public void Dispose()
{
this.Database.Dispose();
this.DatabaseProvider.Dispose();
this.Http.Dispose();
this.Listener.Dispose();
if (this.Server.IsValueCreated)
this.Server.Value.Stop();
GC.SuppressFinalize(this);
}
}