Skip to content

Commit 89ace30

Browse files
committed
Refactor test auth data handling
1 parent 568b3c5 commit 89ace30

15 files changed

Lines changed: 207 additions & 221 deletions

Assets/Plugins/StreamChat/EditorTools/CommandLineParsers/BuildSettingsCommandLineParser.cs

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
4+
using System.IO.Compression;
35
using System.Text;
4-
using StreamChat.EditorTools;
56
using StreamChat.EditorTools.Builders;
67
using StreamChat.Libs.Auth;
78
using StreamChat.Libs.Serialization;
@@ -61,7 +62,7 @@ protected override (BuildSettings buildSettings, AuthCredentials authCredentials
6162
testAuthDataSet.GetAdminData(forceIndex: optionalTestDataIndex));
6263
}
6364

64-
public TestAuthDataSet ParseTestAuthDataSetArg(IDictionary<string, string> args, out int? forceDataSetIndex)
65+
public TestAuthDataSets ParseTestAuthDataSetArg(IDictionary<string, string> args, out int? forceDataSetIndex)
6566
{
6667
if (!args.ContainsKey(StreamBase64TestDataArgKey))
6768
{
@@ -74,9 +75,9 @@ public TestAuthDataSet ParseTestAuthDataSetArg(IDictionary<string, string> args,
7475
}
7576

7677
forceDataSetIndex = GetOptionalTestDataIndex();
77-
var rawTestDataSet = GetTestDataSet(args);
78-
var serializer = new NewtonsoftJsonSerializer();
79-
return serializer.Deserialize<TestAuthDataSet>(rawTestDataSet);
78+
var base64TestAuthDataSet = args[StreamBase64TestDataArgKey];
79+
80+
return DeserializeFromBase64(base64TestAuthDataSet);
8081

8182
int? GetOptionalTestDataIndex()
8283
{
@@ -88,6 +89,48 @@ public TestAuthDataSet ParseTestAuthDataSetArg(IDictionary<string, string> args,
8889
return int.Parse(arg);
8990
}
9091
}
92+
93+
public TestAuthDataSets DeserializeFromBase64(string urlSafeBase64)
94+
{
95+
Debug.Log($"Test Data Set. URL-safe Base 64 encoded length: {urlSafeBase64.Length}");
96+
97+
var base64Set = UrlSafeBase64ToBase64(urlSafeBase64);
98+
var decodedBytes = Convert.FromBase64String(base64Set);
99+
100+
var decodedString = DecompressString(decodedBytes);
101+
Debug.Log($"Test Data Set. Decompressed to UTF8 string length: {decodedString.Length}");
102+
103+
var serializer = new NewtonsoftJsonSerializer();
104+
var testAuthDataSet = serializer.Deserialize<TestAuthDataSets>(decodedString);
105+
Debug.Log($"Test Data Set. Admin sets: {testAuthDataSet.Admins.Length}, User sets: {testAuthDataSet.Users.Length}");
106+
107+
return testAuthDataSet;
108+
}
109+
110+
private static string UrlSafeBase64ToBase64(string urlSafeBase64)
111+
{
112+
var result = urlSafeBase64.Replace('_', '/').Replace('-', '+');
113+
switch(urlSafeBase64.Length % 4) {
114+
case 2: result += "=="; break;
115+
case 3: result += "="; break;
116+
}
117+
118+
return result;
119+
}
120+
121+
private static string DecompressString(byte[] bytes)
122+
{
123+
using (var memoryStream = new MemoryStream(bytes))
124+
{
125+
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
126+
{
127+
using (var reader = new StreamReader(gzipStream, Encoding.UTF8))
128+
{
129+
return reader.ReadToEnd();
130+
}
131+
}
132+
}
133+
}
91134

92135
private static BuildTargetGroup GetBuildTargetGroup(BuildTargetPlatform targetPlatform)
93136
{
@@ -140,11 +183,5 @@ private static ScriptingImplementation GetScriptingImplementation(ScriptingBacke
140183
throw new ArgumentOutOfRangeException(nameof(scriptingBackend), scriptingBackend, null);
141184
}
142185
}
143-
144-
private static string GetTestDataSet(IDictionary<string, string> args)
145-
{
146-
var decodedBytes = Convert.FromBase64String(args[StreamBase64TestDataArgKey]);
147-
return Encoding.UTF8.GetString(decodedBytes);
148-
}
149186
}
150187
}

Assets/Plugins/StreamChat/EditorTools/TestAuthDataSet.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using StreamChat.Libs.Auth;
4+
using UnityEngine;
5+
using Random = UnityEngine.Random;
6+
7+
namespace StreamChat.EditorTools
8+
{
9+
public class TestAuthDataSets
10+
{
11+
public AuthCredentials[] Admins { get; set; }
12+
public AuthCredentials[] Users { get; set; }
13+
14+
public TestAuthDataSets(IEnumerable<AuthCredentials> testAdminData, IEnumerable<AuthCredentials> userSets)
15+
{
16+
Admins = testAdminData.ToArray();
17+
Users = userSets.ToArray();
18+
}
19+
20+
public TestAuthDataSets()
21+
{
22+
23+
}
24+
25+
public AuthCredentials GetAdminData(int? forceIndex = default) => GetDataSet(true, forceIndex);
26+
27+
private AuthCredentials GetDataSet(bool isAdmin, int? forceIndex = default)
28+
{
29+
var sets = isAdmin ? Admins : Users;
30+
if (forceIndex.HasValue)
31+
{
32+
if (forceIndex < sets.Length)
33+
{
34+
return sets[forceIndex.Value];
35+
}
36+
37+
Debug.LogWarning(
38+
$"{nameof(forceIndex)} is out of range -> given: {forceIndex} for ss admin: {isAdmin}, max allowed: {sets.Length - 1}. Using random credentials data instead.");
39+
}
40+
41+
return sets[Random.Range(0, sets.Length)];
42+
}
43+
}
44+
}

Assets/Plugins/StreamChat/EditorTools/TestAuthDataSet.cs.meta renamed to Assets/Plugins/StreamChat/EditorTools/TestAuthDataSets.cs.meta

File renamed without changes.

Assets/Plugins/StreamChat/Tests/LowLevelClient/Integration/BaseIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async void OneTimeTearDown()
4444
/// <summary>
4545
/// Id of other user than currently logged one
4646
/// </summary>
47-
protected static string OtherUserId => StreamTestClients.Instance.OtherUserId;
47+
protected static string SecondUserId => StreamTestClients.Instance.AdminSecondaryCredentials.UserId;
4848

4949
protected static OwnUser LowLevelClientOwnUser => StreamTestClients.Instance.LowLevelClientOwnUser;
5050

Assets/Plugins/StreamChat/Tests/LowLevelClient/Integration/ChannelApiIntegrationTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public IEnumerator Get_or_create_channel_for_list_of_members()
8787
},
8888
new ChannelMemberRequest
8989
{
90-
UserId = OtherUserId
90+
UserId = SecondUserId
9191
},
9292
}
9393
}
@@ -505,7 +505,7 @@ public IEnumerator Mark_single_read_with_specified_message_id()
505505
{
506506
new ChannelMemberRequest
507507
{
508-
UserId = OtherUserId
508+
UserId = SecondUserId
509509
},
510510
new ChannelMemberRequest
511511
{
@@ -600,7 +600,7 @@ public IEnumerator Mark_single_read_without_message_id()
600600
{
601601
new ChannelMemberRequest
602602
{
603-
UserId = OtherUserId
603+
UserId = SecondUserId
604604
},
605605
new ChannelMemberRequest
606606
{
@@ -750,7 +750,7 @@ public IEnumerator Mark_many_read_with_specified_message_id()
750750
{
751751
new ChannelMemberRequest
752752
{
753-
UserId = OtherUserId
753+
UserId = SecondUserId
754754
},
755755
new ChannelMemberRequest
756756
{

Assets/Plugins/StreamChat/Tests/LowLevelClient/Integration/ModerationApiIntegrationTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public IEnumerator Mute_user()
2828
{
2929
TargetIds = new List<string>
3030
{
31-
OtherUserId
31+
SecondUserId
3232
},
3333
Timeout = MuteTimeout
3434
};
@@ -38,7 +38,7 @@ public IEnumerator Mute_user()
3838
yield return muteUserTask.RunAsIEnumerator(response =>
3939
{
4040
var muteInfo = response.Mute;
41-
Assert.AreEqual(muteInfo.Target.Id, OtherUserId);
41+
Assert.AreEqual(muteInfo.Target.Id, SecondUserId);
4242

4343
var calcedTimeout = (muteInfo.Expires - muteInfo.UpdatedAt).Value.TotalMinutes;
4444

@@ -55,7 +55,7 @@ public IEnumerator Unmute_user()
5555
{
5656
TargetIds = new List<string>
5757
{
58-
OtherUserId
58+
SecondUserId
5959
},
6060
};
6161

@@ -79,7 +79,7 @@ public IEnumerator Ban_user()
7979

8080
var banRequest = new BanRequest
8181
{
82-
TargetUserId = OtherUserId,
82+
TargetUserId = SecondUserId,
8383
Id = channelState.Channel.Id,
8484
Type = channelType,
8585
Timeout = BanTimeout
@@ -106,7 +106,7 @@ public IEnumerator Ban_user()
106106

107107
yield return queryBannedUsersTask.RunAsIEnumerator(response =>
108108
{
109-
var userBanInfo = response.Bans.FirstOrDefault(_ => _.User.Id == OtherUserId);
109+
var userBanInfo = response.Bans.FirstOrDefault(_ => _.User.Id == SecondUserId);
110110
Assert.IsNotNull(userBanInfo);
111111

112112
var calcedTimeout = (userBanInfo.Expires - userBanInfo.CreatedAt).Value.TotalMinutes;
@@ -130,7 +130,7 @@ public IEnumerator Unban_user()
130130

131131
var banRequest = new BanRequest
132132
{
133-
TargetUserId = OtherUserId,
133+
TargetUserId = SecondUserId,
134134
Id = channelState.Channel.Id,
135135
Type = channelType,
136136
Timeout = BanTimeout
@@ -156,7 +156,7 @@ public IEnumerator Unban_user()
156156

157157
yield return queryBannedUsersTask.RunAsIEnumerator(response =>
158158
{
159-
var userBanInfo = response.Bans.FirstOrDefault(_ => _.User.Id == OtherUserId);
159+
var userBanInfo = response.Bans.FirstOrDefault(_ => _.User.Id == SecondUserId);
160160
Assert.IsNotNull(userBanInfo);
161161

162162
var calcedTimeout = (userBanInfo.Expires - userBanInfo.CreatedAt).Value.TotalMinutes;
@@ -166,7 +166,7 @@ public IEnumerator Unban_user()
166166

167167
var unbanRequest = new UnbanRequest
168168
{
169-
TargetUserId = OtherUserId,
169+
TargetUserId = SecondUserId,
170170
Id = channelState.Channel.Id,
171171
Type = channelType,
172172
};
@@ -190,7 +190,7 @@ public IEnumerator Unban_user()
190190
var queryBannedUsersTask2 = LowLevelClient.ModerationApi.QueryBannedUsersAsync(queryBannedUsersRequest2);
191191
yield return queryBannedUsersTask2.RunAsIEnumerator(response =>
192192
{
193-
var userBanInfo = response.Bans.FirstOrDefault(_ => _.User.Id == OtherUserId);
193+
var userBanInfo = response.Bans.FirstOrDefault(_ => _.User.Id == SecondUserId);
194194
Assert.IsNull(userBanInfo);
195195
});
196196
}
@@ -311,8 +311,8 @@ private async Task When_user_flagged_expect_response_target_user_id_match_Async(
311311
const string channelType = "messaging";
312312
var channelState = await CreateTempUniqueChannelAsync(channelType, new ChannelGetOrCreateRequest());
313313

314-
var response = await LowLevelClient.ModerationApi.FlagUserAsync(OtherUserId);
315-
Assert.AreEqual(OtherUserId, response.Flag.TargetUser.Id);
314+
var response = await LowLevelClient.ModerationApi.FlagUserAsync(SecondUserId);
315+
Assert.AreEqual(SecondUserId, response.Flag.TargetUser.Id);
316316
}
317317
}
318318
}

Assets/Plugins/StreamChat/Tests/StatefulClient/BaseStateIntegrationTests.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,12 @@ public async void OneTimeTearDown()
3737
protected static StreamChatClient Client => StreamTestClients.Instance.StateClient;
3838

3939
protected int MainThreadId { get; } = Thread.CurrentThread.ManagedThreadId;
40-
41-
// StreamTodo: replace with admin ids fetched from loaded data set
42-
protected const string TestUserId = TestUtils.TestUserId;
43-
protected const string TestAdminId = TestUtils.TestAdminId;
44-
protected const string TestGuestId = TestUtils.TestGuestId;
45-
46-
protected static string OtherUserId => StreamTestClients.Instance.OtherUserId;
47-
48-
protected static IEnumerable<AuthCredentials> OtherAdminUsersCredentials
49-
=> StreamTestClients.Instance.OtherUserCredentials;
40+
41+
protected AuthCredentials AdminPrimaryCredentials => StreamTestClients.Instance.AdminPrimaryCredentials;
42+
protected AuthCredentials AdminSecondaryCredentials => StreamTestClients.Instance.AdminSecondaryCredentials;
43+
44+
protected AuthCredentials UserPrimaryCredentials => StreamTestClients.Instance.UserPrimaryCredentials;
45+
protected AuthCredentials UserSecondaryCredentials => StreamTestClients.Instance.UserSecondaryCredentials;
5046

5147
protected int GetCurrentThreadId() => Thread.CurrentThread.ManagedThreadId;
5248

0 commit comments

Comments
 (0)