Esempio n. 1
0
        public async Task SimpleAuthTest()
        {
            var database = new Butterfly.Core.Database.Memory.MemoryDatabase();

            //var database = new Butterfly.MySql.MySqlDatabase("Server=127.0.0.1;Uid=test;Pwd=test!123;Database=butterfly_auth_test");
            await TruncateData(database);

            await database.CreateFromResourceFileAsync(Assembly.GetExecutingAssembly(), "Butterfly.Core.Auth.Test.db.sql");

            database.SetDefaultValue("id", tableName => Guid.NewGuid().ToString());
            database.SetDefaultValue("created_at", tableName => DateTime.Now.ToUnixTimestamp());
            database.SetDefaultValue("updated_at", tableName => DateTime.Now.ToUnixTimestamp());
            database.AddInputPreprocessor(BaseDatabase.RemapTypeInputPreprocessor <DateTime>(dateTime => dateTime.ToUnixTimestamp()));

            AuthManager authManager = new AuthManager(database, onForgotPassword: user => {
                logger.Debug($"onForgotPassword():user={user}");
                return(Task.FromResult(0));
            });
            UserRefToken registerAuthToken = await authManager.RegisterAsync(new {
                username   = "******",
                first_name = "John",
                last_name  = "Smith",
                email      = "*****@*****.**",
                password   = "******"
            });

            AuthToken authToken = await authManager.AuthenticateAsync(UserRefTokenAuthenticator.AUTH_TYPE, registerAuthToken.id);

            await authManager.ForgotPasswordAsync("johnsmith");

            await Task.Delay(30000);
        }
Esempio n. 2
0
        public async Task RegistrationTests()
        {
            var database = new Butterfly.Db.Memory.MemoryDatabase();

            await database.CreateFromResourceFileAsync(Assembly.GetExecutingAssembly(), "Butterfly.Auth.Test.butterfly_auth_test.sql");

            database.SetDefaultValue("id", tableName => Guid.NewGuid().ToString());
            database.SetDefaultValue("created_at", tableName => DateTime.Now.ToUnixTimestamp());
            database.SetDefaultValue("updated_at", tableName => DateTime.Now.ToUnixTimestamp());
            database.AddInputPreprocessor(BaseDatabase.RemapTypeInputPreprocessor <DateTime>(dateTime => dateTime.ToUnixTimestamp()));

            // Create a single instance of AuthManager
            AuthManager authManager = new AuthManager(database);

            // Register a valid user
            UserRefToken registerAuthToken1 = await authManager.RegisterAsync(new {
                username   = "******",
                first_name = "John",
                last_name  = "Smith",
                email      = "*****@*****.**",
                phone      = "+13162105368",
                password   = "******"
            });

            // Verify the returned auth token can be authenticated
            var authToken1 = await authManager.AuthenticateAsync(UserRefTokenAuthenticator.AUTH_TYPE, registerAuthToken1.id);

            // Register an invalid email
            Exception exception2 = null;

            try {
                UserRefToken registerAuthToken2 = await authManager.RegisterAsync(new {
                    username   = "******",
                    first_name = "John",
                    last_name  = "Smith",
                    email      = "john",
                    phone      = "+13162105368",
                    password   = "******"
                });
            }
            catch (Exception e) {
                exception2 = e;
            }
            Assert.IsTrue(exception2.Message.Contains("Email address must contain"));

            // Register an invalid phone
            Exception exception3 = null;

            try {
                UserRefToken registerAuthToken2 = await authManager.RegisterAsync(new {
                    username   = "******",
                    first_name = "John",
                    last_name  = "Smith",
                    email      = "*****@*****.**",
                    phone      = "123",
                    password   = "******"
                });
            }
            catch (Exception e) {
                exception3 = e;
            }
            Assert.IsTrue(exception3.Message.Contains("Invalid phone number"));

            // Register a duplicate username
            Exception exception4 = null;

            try {
                UserRefToken registerAuthToken2 = await authManager.RegisterAsync(new {
                    username   = "******",
                    first_name = "John",
                    last_name  = "Smith",
                    email      = "*****@*****.**",
                    phone      = "+13162105368",
                    password   = "******"
                });
            }
            catch (Exception e) {
                exception4 = e;
            }
            Assert.IsTrue(exception4.Message.Contains("unavailable"));

            // Register without a password
            Exception exception5 = null;

            try {
                UserRefToken registerAuthToken2 = await authManager.RegisterAsync(new {
                    username   = "******",
                    first_name = "John",
                    last_name  = "Smith",
                    email      = "*****@*****.**",
                    phone      = "+13162105368"
                });
            }
            catch (Exception e) {
                exception5 = e;
            }
            Assert.IsTrue(exception5.Message.Contains("password cannot be null"));
        }