[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the ListUsers() Implementation #77

Merged
merged 9 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion FirebaseAdmin/FirebaseAdmin.IntegrationTests/FirebaseAuthTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using FirebaseAdmin;
using FirebaseAdmin.Auth;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Util;
Expand Down Expand Up @@ -284,6 +284,45 @@ public async Task DeleteUserNonExistingUid()
async () => await FirebaseAuth.DefaultInstance.DeleteUserAsync("non.existing"));
}

[Fact]
public async Task ListUsers()
{
var users = new List<string>();
try
{
for (int i = 0; i < 3; i++)
{
var user = await FirebaseAuth.DefaultInstance.CreateUserAsync(new UserRecordArgs()
{
Password = "password",
});
users.Add(user.Uid);
}

var pagedEnumerable = FirebaseAuth.DefaultInstance.ListUsersAsync(null);
var enumerator = pagedEnumerable.GetEnumerator();

var listedUsers = new List<string>();
while (await enumerator.MoveNext())
{
var uid = enumerator.Current.Uid;
if (users.Contains(uid) && !listedUsers.Contains(uid))
{
listedUsers.Add(uid);
Assert.NotNull(enumerator.Current.PasswordHash);
Assert.NotNull(enumerator.Current.PasswordSalt);
}
}

Assert.Equal(3, listedUsers.Count);
}
finally
{
var deleteTasks = users.Select((uid) => FirebaseAuth.DefaultInstance.DeleteUserAsync(uid));
await Task.WhenAll(deleteTasks);
}
}

private static async Task<string> SignInWithCustomTokenAsync(string customToken)
{
var rb = new Google.Apis.Requests.RequestBuilder()
Expand Down
155 changes: 155 additions & 0 deletions FirebaseAdmin/FirebaseAdmin.Tests/Auth/ExportedUserRecordTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright 2019, Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using Xunit;

namespace FirebaseAdmin.Auth.Tests
{
public class ExportedUserRecordTest
{
[Fact]
public void NullResponse()
{
Assert.Throws<ArgumentException>(() => new ExportedUserRecord(null));
}

[Fact]
public void NullUid()
{
Assert.Throws<ArgumentException>(() => new ExportedUserRecord(
new GetAccountInfoResponse.User()
{
UserId = null,
}));
}

[Fact]
public void EmptyUid()
{
Assert.Throws<ArgumentException>(() => new ExportedUserRecord(
new GetAccountInfoResponse.User()
{
UserId = string.Empty,
}));
}

[Fact]
public void UidOnly()
{
var user = new ExportedUserRecord(new GetAccountInfoResponse.User()
{
UserId = "user1",
});

Assert.Equal("user1", user.Uid);
Assert.Null(user.DisplayName);
Assert.Null(user.Email);
Assert.Null(user.PhoneNumber);
Assert.Null(user.PhotoUrl);
Assert.Equal("firebase", user.ProviderId);
Assert.False(user.Disabled);
Assert.False(user.EmailVerified);
Assert.Equal(UserRecord.UnixEpoch, user.TokensValidAfterTimestamp);
Assert.Empty(user.CustomClaims);
Assert.Empty(user.ProviderData);
Assert.NotNull(user.UserMetaData);
Assert.Null(user.UserMetaData.CreationTimestamp);
Assert.Null(user.UserMetaData.LastSignInTimestamp);
Assert.Null(user.PasswordHash);
Assert.Null(user.PasswordSalt);
}

[Fact]
public void AllProperties()
{
var response = new GetAccountInfoResponse.User()
{
UserId = "user1",
DisplayName = "Test User",
Email = "user@domain.com",
PhoneNumber = "+11234567890",
PhotoUrl = "https://domain.com/user.png",
Disabled = true,
EmailVerified = true,
ValidSince = 3600,
CreatedAt = 100,
LastLoginAt = 150,
CustomClaims = @"{""admin"": true, ""level"": 10}",
PasswordHash = "secret",
PasswordSalt = "nacl",
Providers = new List<GetAccountInfoResponse.Provider>()
{
new GetAccountInfoResponse.Provider()
{
ProviderID = "google.com",
UserId = "googleuid",
},
new GetAccountInfoResponse.Provider()
{
ProviderID = "other.com",
UserId = "otheruid",
DisplayName = "Other Name",
Email = "user@other.com",
PhotoUrl = "https://other.com/user.png",
PhoneNumber = "+10987654321",
},
},
};
var user = new ExportedUserRecord(response);

Assert.Equal("user1", user.Uid);
Assert.Equal("Test User", user.DisplayName);
Assert.Equal("user@domain.com", user.Email);
Assert.Equal("+11234567890", user.PhoneNumber);
Assert.Equal("https://domain.com/user.png", user.PhotoUrl);
Assert.Equal("firebase", user.ProviderId);
Assert.True(user.Disabled);
Assert.True(user.EmailVerified);
Assert.Equal(UserRecord.UnixEpoch.AddSeconds(3600), user.TokensValidAfterTimestamp);
Assert.Equal("secret", user.PasswordHash);
Assert.Equal("nacl", user.PasswordSalt);

var claims = new Dictionary<string, object>()
{
{ "admin", true },
{ "level", 10L },
};
Assert.Equal(claims, user.CustomClaims);

Assert.Equal(2, user.ProviderData.Length);
var provider = user.ProviderData[0];
Assert.Equal("google.com", provider.ProviderId);
Assert.Equal("googleuid", provider.Uid);
Assert.Null(provider.DisplayName);
Assert.Null(provider.Email);
Assert.Null(provider.PhoneNumber);
Assert.Null(provider.PhotoUrl);

provider = user.ProviderData[1];
Assert.Equal("other.com", provider.ProviderId);
Assert.Equal("otheruid", provider.Uid);
Assert.Equal("Other Name", provider.DisplayName);
Assert.Equal("user@other.com", provider.Email);
Assert.Equal("+10987654321", provider.PhoneNumber);
Assert.Equal("https://other.com/user.png", provider.PhotoUrl);

var metadata = user.UserMetaData;
Assert.NotNull(metadata);
Assert.Equal(UserRecord.UnixEpoch.AddMilliseconds(100), metadata.CreationTimestamp);
Assert.Equal(UserRecord.UnixEpoch.AddMilliseconds(150), metadata.LastSignInTimestamp);
}
}
}
Loading