-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathTemporaryUserBuilder.cs
More file actions
114 lines (102 loc) · 4.17 KB
/
TemporaryUserBuilder.cs
File metadata and controls
114 lines (102 loc) · 4.17 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
// Copyright 2020, 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 System.Linq;
using System.Security.Policy;
using System.Threading;
using System.Threading.Tasks;
using FirebaseAdmin.Auth;
namespace FirebaseAdmin.IntegrationTests.Auth
{
/// <summary>
/// A utility for creating temporary user accounts during tests. Keeps track of the user
/// accounts created, and deletes them on dispose. Other user accounts created outside this
/// class can be marked for deletion using the <see cref="AddUid(string)"/> method. This class
/// deletes user accounts in an idempotent manner. Therefore it's safe to delete any of the
/// user accounts created by this class before this instance is disposed. This class is not
/// thread safe. Any concurrent usage should be synchronized accordingly.
/// <summary>
public sealed class TemporaryUserBuilder : IDisposable
{
private readonly ISet<string> userIds = new HashSet<string>();
private readonly AbstractFirebaseAuth auth;
public TemporaryUserBuilder(AbstractFirebaseAuth auth)
{
this.auth = auth;
}
public static UserRecordArgs RandomUserRecordArgs()
{
var uid = Guid.NewGuid().ToString().Replace("-", string.Empty);
var rand = new Random();
var phoneDigits = Enumerable.Range(0, 10).Select(_ => rand.Next(10));
return new UserRecordArgs()
{
Uid = uid,
Email = $"test{uid.Substring(0, 12)}@example.{uid.Substring(12)}.com",
PhoneNumber = $"+1{string.Join(string.Empty, phoneDigits)}",
DisplayName = "Random User",
PhotoUrl = "https://example.com/photo.png",
Password = "password",
};
}
public static UserRecordArgs RandomUserRecordArgsWithMfa()
{
var userArgs = RandomUserRecordArgs();
userArgs.EmailVerified = true;
userArgs.Mfa = new List<MfaEnrollmentArgs>()
{
new MfaEnrollmentArgs
{
PhoneInfo = userArgs.PhoneNumber,
DisplayName = "test factor",
EnrolledAt = DateTime.UtcNow,
MfaFactorId = MfaFactorIdType.Phone,
},
};
return userArgs;
}
public async Task<UserRecord> CreateRandomUserAsync()
{
return await this.CreateUserAsync(RandomUserRecordArgs());
}
public async Task<UserRecord> CreateRandomUserMithMfaAsync()
{
return await this.CreateUserAsync(RandomUserRecordArgsWithMfa());
}
public async Task<UserRecord> CreateUserAsync(UserRecordArgs args)
{
// Make sure we never create more than 1000 users in a single instance.
// This allows us to delete all user accounts with a single call to DeleteUsers().
// Should not ever occur in practice.
if (this.userIds.Count() >= 1000)
{
throw new InvalidOperationException("Maximum number of users reached.");
}
var user = await this.auth.CreateUserAsync(args);
this.AddUid(user.Uid);
return user;
}
public bool AddUid(string uid)
{
return this.userIds.Add(uid);
}
public void Dispose()
{
Thread.Sleep(1000); // DeleteUsers is rate limited at 1qps.
this.auth.DeleteUsersAsync(this.userIds.ToList()).Wait();
this.userIds.Clear();
}
}
}