Skip to content

Commit 1cd6835

Browse files
committed
Stonger validation for pageSize and token
1 parent bef5439 commit 1cd6835

File tree

3 files changed

+66
-37
lines changed

3 files changed

+66
-37
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/Auth/FirebaseUserManagerTest.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
using System.Net.Http;
2020
using System.Threading.Tasks;
2121
using FirebaseAdmin.Tests;
22-
using Google.Api.Gax;
2322
using Google.Apis.Auth.OAuth2;
2423
using Google.Apis.Json;
2524
using Newtonsoft.Json.Linq;
@@ -577,7 +576,23 @@ public async Task ListUsersAsRawResponses()
577576
}
578577

579578
[Fact]
580-
public void ListUsersPageSizeTooLarge()
579+
public async Task ListUsersReadPageSizeTooLarge()
580+
{
581+
var handler = new MockMessageHandler()
582+
{
583+
Response = ListUsersResponse,
584+
};
585+
var userManager = this.CreateFirebaseUserManager(handler);
586+
var pagedEnumerable = userManager.ListUsers(null);
587+
588+
await Assert.ThrowsAsync<ArgumentException>(
589+
async () => await pagedEnumerable.ReadPageAsync(1001));
590+
591+
Assert.Empty(handler.Requests);
592+
}
593+
594+
[Fact]
595+
public void ListUsersOptionsPageSizeTooLarge()
581596
{
582597
var handler = new MockMessageHandler()
583598
{
@@ -594,7 +609,7 @@ public void ListUsersPageSizeTooLarge()
594609
}
595610

596611
[Fact]
597-
public void ListUsersPageSizeTooSmall()
612+
public void ListUsersOptionsPageSizeTooSmall()
598613
{
599614
var handler = new MockMessageHandler()
600615
{
@@ -615,7 +630,7 @@ public void ListUsersPageSizeTooSmall()
615630
}
616631

617632
[Fact]
618-
public void ListUsersPageTokenEmpty()
633+
public void ListUsersOptionsPageTokenEmpty()
619634
{
620635
var handler = new MockMessageHandler()
621636
{

FirebaseAdmin/FirebaseAdmin/Auth/ExportedUserRecords.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace FirebaseAdmin.Auth
2222
public sealed class ExportedUserRecords
2323
{
2424
/// <summary>
25-
/// Gets the token representing the next page of users.
25+
/// Gets the token representing the next page of users. Null if there are no more pages.
2626
/// </summary>
2727
public string NextPageToken { get; internal set; }
2828

FirebaseAdmin/FirebaseAdmin/Auth/ListUsersRequest.cs

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,8 @@ private ListUsersRequest(
4343
this.baseUrl = baseUrl;
4444
this.httpClient = httpClient;
4545
this.RequestParameters = new Dictionary<string, IParameter>();
46-
this.SetPageSize(options.PageSize ?? MaxListUsersResults);
47-
var pageToken = options.PageToken;
48-
if (pageToken != null)
49-
{
50-
this.SetPageToken(pageToken);
51-
}
46+
this.SetPageSize(options.PageSize);
47+
this.SetPageToken(options.PageToken);
5248
}
5349

5450
public string MethodName => "ListUsers";
@@ -61,16 +57,6 @@ private ListUsersRequest(
6157

6258
public IClientService Service { get; }
6359

64-
public void SetPageSize(int pageSize)
65-
{
66-
this.AddOrUpdate("maxResults", pageSize.ToString());
67-
}
68-
69-
public void SetPageToken(string pageToken)
70-
{
71-
this.AddOrUpdate("nextPageToken", pageToken);
72-
}
73-
7460
public HttpRequestMessage CreateRequest(bool? overrideGZipEnabled = null)
7561
{
7662
var queryParameters = string.Join("&", this.RequestParameters.Select(
@@ -121,6 +107,47 @@ public ExportedUserRecords Execute()
121107
return this.ExecuteAsync().Result;
122108
}
123109

110+
internal void SetPageSize(int? pageSize)
111+
{
112+
this.AddOrUpdate("maxResults", CheckPageSize(pageSize).ToString());
113+
}
114+
115+
internal void SetPageToken(string pageToken)
116+
{
117+
if (pageToken != null)
118+
{
119+
this.AddOrUpdate("nextPageToken", CheckPageToken(pageToken));
120+
}
121+
else
122+
{
123+
this.RequestParameters.Remove("nextPageToken");
124+
}
125+
}
126+
127+
private static int CheckPageSize(int? pageSize)
128+
{
129+
if (pageSize > MaxListUsersResults)
130+
{
131+
throw new ArgumentException("Page size must not exceed 1000.");
132+
}
133+
else if (pageSize <= 0)
134+
{
135+
throw new ArgumentException("Page size must be positive.");
136+
}
137+
138+
return pageSize ?? MaxListUsersResults;
139+
}
140+
141+
private static string CheckPageToken(string token)
142+
{
143+
if (token == string.Empty)
144+
{
145+
throw new ArgumentException("Page token must not be empty.");
146+
}
147+
148+
return token;
149+
}
150+
124151
private void AddOrUpdate(string paramName, string value)
125152
{
126153
var parameter = new Parameter()
@@ -190,7 +217,7 @@ private async Task<HttpResponseMessage> SendAsync(
190217
/// Factory class that validates arguments, and then creates new instances of the
191218
/// <see cref="ListUsersRequest"/> class.
192219
/// </summary>
193-
internal class Factory
220+
internal sealed class Factory
194221
{
195222
private readonly string baseUrl;
196223
private readonly HttpClient httpClient;
@@ -203,22 +230,9 @@ internal Factory(
203230
this.httpClient = httpClient;
204231
this.options = new ListUsersOptions()
205232
{
206-
PageSize = options?.PageSize ?? MaxListUsersResults,
207-
PageToken = options?.PageToken,
233+
PageSize = CheckPageSize(options?.PageSize),
234+
PageToken = CheckPageToken(options?.PageToken),
208235
};
209-
210-
if (this.options.PageSize > MaxListUsersResults)
211-
{
212-
throw new ArgumentException("Page size must not exceed 1000.");
213-
}
214-
else if (this.options.PageSize <= 0)
215-
{
216-
throw new ArgumentException("Page size must be positive.");
217-
}
218-
else if (this.options.PageToken == string.Empty)
219-
{
220-
throw new ArgumentException("Initial page token must not be empty.");
221-
}
222236
}
223237

224238
internal ListUsersRequest Create()

0 commit comments

Comments
 (0)