Exemple #1
0
 public void App_Login_Anonymous()
 {
     SyncTestHelpers.RunBaasTestAsync(async() =>
     {
         var user = await DefaultApp.LogInAsync(Credentials.Anonymous());
     });
 }
        public void User_LinkCredentials_MultipleTimes_AllowsLoginWithAllCredentials()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await DefaultApp.LogInAsync(Credentials.Anonymous());

                var email = SyncTestHelpers.GetVerifiedUsername();
                await DefaultApp.EmailPasswordAuth.RegisterUserAsync(email, SyncTestHelpers.DefaultPassword);
                var linkedUser1 = await user.LinkCredentialsAsync(Credentials.EmailPassword(email, SyncTestHelpers.DefaultPassword));
                Assert.That(linkedUser1.Id, Is.EqualTo(user.Id));

                var functionId  = Guid.NewGuid().ToString();
                var linkedUser2 = await user.LinkCredentialsAsync(Credentials.Function(new { realmCustomAuthFuncUserId = functionId }));
                Assert.That(linkedUser2.Id, Is.EqualTo(user.Id));

                var emailPasswordUser = await DefaultApp.LogInAsync(Credentials.EmailPassword(email, SyncTestHelpers.DefaultPassword));
                Assert.That(emailPasswordUser.Id, Is.EqualTo(user.Id));

                var functionUser = await DefaultApp.LogInAsync(Credentials.Function(new { realmCustomAuthFuncUserId = functionId }));
                Assert.That(functionUser.Id, Is.EqualTo(user.Id));

                Assert.That(user.Identities, Has.Length.EqualTo(3));
                Assert.That(user.Identities[0].Provider, Is.EqualTo(Credentials.AuthProvider.Anonymous));
                Assert.That(user.Identities[0].Id, Is.Not.Null);

                Assert.That(user.Identities[1].Provider, Is.EqualTo(Credentials.AuthProvider.EmailPassword));
                Assert.That(user.Identities[1].Id, Is.Not.Null);

                Assert.That(user.Identities[2].Provider, Is.EqualTo(Credentials.AuthProvider.Function));
                Assert.That(user.Identities[2].Id, Is.EqualTo(functionId));
            });
        }
        public void UserApiKeys_CanLoginWithReenabledKey()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user   = await GetUserAsync();
                var apiKey = await user.ApiKeys.CreateAsync("my-api-key");

                await user.ApiKeys.DisableAsync(apiKey.Id);

                var credentials = Credentials.ApiKey(apiKey.Value);

                var ex = await TestHelpers.AssertThrows <AppException>(() => DefaultApp.LogInAsync(credentials));
                Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
                Assert.That(ex.HelpLink, Does.Contain("logs?co_id="));
                Assert.That(ex.Message, Is.EqualTo("AuthError: invalid API key"));

                await user.ApiKeys.EnableAsync(apiKey.Id);

                var apiKeyUser = await DefaultApp.LogInAsync(credentials);

                Assert.That(apiKeyUser.Id, Is.EqualTo(user.Id));

                Assert.That(apiKeyUser.Provider, Is.EqualTo(Credentials.AuthProvider.ApiKey));
                Assert.That(apiKeyUser.RefreshToken, Is.Not.EqualTo(user.RefreshToken));
            });
        }
        public void User_LinkCredentials_AllowsLoginWithNewCredentials()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await DefaultApp.LogInAsync(Credentials.Anonymous());

                Assert.That(user.Identities, Has.Length.EqualTo(1));
                Assert.That(user.Identities[0].Provider, Is.EqualTo(Credentials.AuthProvider.Anonymous));
                Assert.That(user.Identities[0].Id, Is.Not.Null);

                var email = SyncTestHelpers.GetVerifiedUsername();
                await DefaultApp.EmailPasswordAuth.RegisterUserAsync(email, SyncTestHelpers.DefaultPassword);
                var linkedUser = await user.LinkCredentialsAsync(Credentials.EmailPassword(email, SyncTestHelpers.DefaultPassword));

                Assert.That(user.Identities, Has.Length.EqualTo(2));
                Assert.That(user.Identities[1].Provider, Is.EqualTo(Credentials.AuthProvider.EmailPassword));
                Assert.That(user.Identities[1].Id, Is.Not.Null);

                Assert.That(linkedUser.Identities, Has.Length.EqualTo(2));
                Assert.That(linkedUser.Id, Is.EqualTo(user.Id));
                Assert.That(linkedUser.Identities, Is.EquivalentTo(user.Identities));

                var emailPasswordUser = await DefaultApp.LogInAsync(Credentials.EmailPassword(email, SyncTestHelpers.DefaultPassword));

                Assert.That(emailPasswordUser.Id, Is.EqualTo(user.Id));
                Assert.That(emailPasswordUser.Identities, Is.EquivalentTo(user.Identities));
            });
        }
        public void User_Facebook_LogsInAndReadsDataFromFacebook()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                const string fbToken = "EAAFYw2aZAL1EBAHBBH22XBDZAutJFQ65KxH0bZAexYul5KtsHcjhI722XYEr4jKlaNvlosFsdZCT8dGUQNy2euZB684mpvtIIJEWWYMoH66bbEbKIrHRWqZBC8KMpSscoyzhFTJMpDYsrIilZBRN1A6bicXGaUNXVz5A0ucyZB7WkmQ8uUmdRWel9q6S8BJH3ZBCZAzWtcZCYmgEwZDZD";
                var credentials      = Credentials.Facebook(fbToken);
                var user             = await DefaultApp.LogInAsync(credentials);

                Assert.That(user.Id, Is.Not.Null);

                Assert.That(user.Profile.FirstName, Is.Not.Null);
                Assert.That(user.Profile.LastName, Is.Not.Null);
            });
        }
        public void UserApiKeys_CanLoginWithGeneratedKey()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user   = await GetUserAsync();
                var apiKey = await user.ApiKeys.CreateAsync("my-api-key");

                var credentials = Credentials.ApiKey(apiKey.Value);
                var apiKeyUser  = await DefaultApp.LogInAsync(credentials);

                Assert.That(apiKeyUser.Id, Is.EqualTo(user.Id));

                Assert.That(apiKeyUser.Provider, Is.EqualTo(Credentials.AuthProvider.ApiKey));
                Assert.That(apiKeyUser.RefreshToken, Is.Not.EqualTo(user.RefreshToken));
            });
        }
        public void User_LinkCredentials_WhenInUse_Throws()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var existingEmail = SyncTestHelpers.GetVerifiedUsername();
                await DefaultApp.EmailPasswordAuth.RegisterUserAsync(existingEmail, SyncTestHelpers.DefaultPassword);
                var emailUser = await DefaultApp.LogInAsync(Credentials.EmailPassword(existingEmail, SyncTestHelpers.DefaultPassword));

                var anonUser = await DefaultApp.LogInAsync(Credentials.Anonymous());

                var ex = await TestHelpers.AssertThrows <AppException>(() => anonUser.LinkCredentialsAsync(Credentials.EmailPassword(existingEmail, SyncTestHelpers.DefaultPassword)));

                Assert.That(ex.StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
                Assert.That(ex.Message, Does.Contain("a user already exists with the specified provider"));
            });
        }
        public void EmailPasswordRegisterUser_Works()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var username = SyncTestHelpers.GetVerifiedUsername();
                await DefaultApp.EmailPasswordAuth.RegisterUserAsync(username, SyncTestHelpers.DefaultPassword);

                var user = await DefaultApp.LogInAsync(Credentials.EmailPassword(username, SyncTestHelpers.DefaultPassword));

                Assert.That(user, Is.Not.Null);
                Assert.That(user.State, Is.EqualTo(UserState.LoggedIn));
                Assert.That(user.Provider, Is.EqualTo(Credentials.AuthProvider.EmailPassword));
                Assert.That(user.AccessToken, Is.Not.Empty);
                Assert.That(user.RefreshToken, Is.Not.Empty);

                Assert.That(DefaultApp.CurrentUser, Is.EqualTo(user));
            });
        }
        public void User_JWT_LogsInAndReadsDataFromToken()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                const string token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxMjM0NTY3ODkwIiwic3ViIjoiMTIzNDU2Nzg5MCIsIm5hbWUiOnsiZmlyc3QiOiJKb2huIiwibGFzdCI6IkRvZSJ9LCJqb2JUaXRsZSI6IkJyZWFrZXIgb2YgdGhpbmdzIiwiZW1haWwiOiJqb2huQGRvZS5jb20iLCJwaWN0dXJlVXJsIjoiaHR0cHM6Ly9kb2UuY29tL215cGljdHVyZSIsImdlbmRlciI6Im90aGVyIiwiYmlydGhkYXkiOiIxOTM0LTA1LTE1IiwibWluQWdlIjoiODAiLCJtYXhBZ2UiOiI5MCIsImlhdCI6MTUxNjIzOTAyMiwiZXhwIjoyMDE2MjM5MDIyLCJhdWQiOiJteS1hdWRpZW5jZSJ9.B6u3SkU-pzCH_LA_HsevAJF1EI1LbAOfL6GP3bhjVpP4FBtrmZYQD_b7Z_wJLE0vaffX1eN6U_vE9t26bmXz2ig4jJRmbg7Kx9ka1BkcE7MF9nmdC90ffHgNBvU40yKpMBtVL9VNQCe-F6mSvUqpox2tQQpNKaXf8yQslAf_tfvqTvF0mPXnqU1v_5KtieMybOb7O8nV6LITrjsAA5ff4spWSgcskjXcyjq6DIdWbLlVJycodr-MjKu94fNXXsBLf0iK5XHYpL1Bs-ILs494_aK_Pf2GD3pYa56XjqN-nO_cYbIxzmsBkNtAp0hvg_Gp0O6QFi66Qkr7ORbkRasGAg";
                var credentials    = Credentials.JWT(token);
                var user           = await DefaultApp.LogInAsync(credentials);

                Assert.That(user.Profile.FirstName, Is.EqualTo("John"));
                Assert.That(user.Profile.LastName, Is.EqualTo("Doe"));
                Assert.That(user.Profile.Email, Is.EqualTo("*****@*****.**"));
                Assert.That(user.Profile.Birthday, Is.EqualTo("1934-05-15"));
                Assert.That(user.Profile.Gender, Is.EqualTo("other"));
                Assert.That(user.Profile.MinAge, Is.EqualTo("80"));
                Assert.That(user.Profile.MaxAge, Is.EqualTo("90"));
                Assert.That(user.Profile.PictureUrl.AbsoluteUri, Is.EqualTo("https://doe.com/mypicture"));

                // TODO: add other checks once https://github.com/realm/realm-core/issues/4131 is implemented.
            });
        }
        public void User_LinkCredentials_MultipleTimesSameCredentials_IsNoOp()
        {
            SyncTestHelpers.RunBaasTestAsync(async() =>
            {
                var user = await DefaultApp.LogInAsync(Credentials.Anonymous());

                var functionId = Guid.NewGuid().ToString();
                var linkedUser = await user.LinkCredentialsAsync(Credentials.Function(new { realmCustomAuthFuncUserId = functionId }));
                Assert.That(linkedUser.Id, Is.EqualTo(user.Id));

                var sameLinkedUser = await user.LinkCredentialsAsync(Credentials.Function(new { realmCustomAuthFuncUserId = functionId }));
                Assert.That(sameLinkedUser.Id, Is.EqualTo(user.Id));

                var functionUser = await DefaultApp.LogInAsync(Credentials.Function(new { realmCustomAuthFuncUserId = functionId }));
                Assert.That(functionUser.Id, Is.EqualTo(user.Id));

                Assert.That(user.Identities, Has.Length.EqualTo(2));
                Assert.That(user.Identities[1].Id, Is.EqualTo(functionId));
                Assert.That(user.Identities[1].Provider, Is.EqualTo(Credentials.AuthProvider.Function));
            });
        }