Android Open Source - couchbase-lite-android Auth Test






From Project

Back to project page couchbase-lite-android.

License

The source code is released under:

Apache License

If you think the Android project couchbase-lite-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.couchbase.lite;
//from w ww . j a v a2 s  .c om
import com.couchbase.lite.auth.Authenticator;
import com.couchbase.lite.auth.AuthenticatorFactory;
import com.couchbase.lite.auth.BasicAuthenticator;
import com.couchbase.lite.auth.PersonaAuthorizer;
import com.couchbase.lite.auth.TokenAuthenticator;
import com.couchbase.lite.util.Log;

import junit.framework.Assert;
import junit.framework.TestCase;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class AuthTest extends TestCase {

    public void testParsePersonaAssertion() {
        try {
            Log.d(Database.TAG, "testParsePersonaAssertion");
            String sampleAssertion = "eyJhbGciOiJSUzI1NiJ9.eyJwdWJsaWMta2V5Ijp7ImFsZ29yaXRobSI6IkRTIiwieSI6ImNhNWJiYTYzZmI4MDQ2OGE0MjFjZjgxYTIzN2VlMDcwYTJlOTM4NTY0ODhiYTYzNTM0ZTU4NzJjZjllMGUwMDk0ZWQ2NDBlOGNhYmEwMjNkYjc5ODU3YjkxMzBlZGNmZGZiNmJiNTUwMWNjNTk3MTI1Y2NiMWQ1ZWQzOTVjZTMyNThlYjEwN2FjZTM1ODRiOWIwN2I4MWU5MDQ4NzhhYzBhMjFlOWZkYmRjYzNhNzNjOTg3MDAwYjk4YWUwMmZmMDQ4ODFiZDNiOTBmNzllYzVlNDU1YzliZjM3NzFkYjEzMTcxYjNkMTA2ZjM1ZDQyZmZmZjQ2ZWZiZDcwNjgyNWQiLCJwIjoiZmY2MDA0ODNkYjZhYmZjNWI0NWVhYjc4NTk0YjM1MzNkNTUwZDlmMWJmMmE5OTJhN2E4ZGFhNmRjMzRmODA0NWFkNGU2ZTBjNDI5ZDMzNGVlZWFhZWZkN2UyM2Q0ODEwYmUwMGU0Y2MxNDkyY2JhMzI1YmE4MWZmMmQ1YT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rWywsTzMTu3t8AzMz2MJgGjvQmx49QA~eyJhbGciOiJEUzEyOCJ9.eyJleHAiOjEzNTgyOTY0Mzg0OTUsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDk4NC8ifQ.4FV2TrUQffDya0MOxOQlzJQbDNvCPF2sfTIJN7KOLvvlSFPknuIo5g";
            Map<String, Object> result = PersonaAuthorizer.parseAssertion(sampleAssertion);
            String email = (String) result.get(PersonaAuthorizer.ASSERTION_FIELD_EMAIL);
            String origin = (String) result.get(PersonaAuthorizer.ASSERTION_FIELD_ORIGIN);

            Assert.assertEquals(email, "jens@mooseyard.com");
            Assert.assertEquals(origin, "http://localhost:4984/");
            Assert.assertEquals(PersonaAuthorizer.registerAssertion(sampleAssertion), email);

            URL originURL = new URL(origin);
            String gotAssertion = PersonaAuthorizer.assertionForEmailAndSite(email, originURL);
            Assert.assertEquals(gotAssertion, sampleAssertion);

            // variant form of URL
            originURL = new URL("Http://LocalHost:4984/");
            gotAssertion = PersonaAuthorizer.assertionForEmailAndSite(email, originURL);
            Assert.assertEquals(sampleAssertion, gotAssertion);

            PersonaAuthorizer auth = new PersonaAuthorizer(email);
            Assert.assertEquals(email, auth.getEmailAddress());
            Assert.assertEquals(null, auth.assertionForSite(originURL));
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    public void testAuthenticatorFactory() {
        Authenticator basicAuth = AuthenticatorFactory.createBasicAuthenticator("username", "password");
        Assert.assertNotNull(basicAuth);
        Assert.assertTrue(basicAuth instanceof BasicAuthenticator);

        Authenticator facebookAuth = AuthenticatorFactory.createFacebookAuthenticator("DUMMY_TOKEN");
        Assert.assertNotNull(facebookAuth);
        Assert.assertTrue(facebookAuth instanceof TokenAuthenticator);

        Authenticator personalAuth = AuthenticatorFactory.createPersonaAuthenticator("DUMMY_ASSERTION", null);
        Assert.assertNotNull(personalAuth);
        Assert.assertTrue(personalAuth instanceof TokenAuthenticator);
    }

    public void testTokenAuthenticator() {
        String loginPath = "_facebook";
        Map<String, String> params = new HashMap<String, String>();
        params.put("access_token", "facebookaccesstoken");
        TokenAuthenticator tokenAuth = new TokenAuthenticator(loginPath, params);

        Map<String, String> tokenAuthParams = tokenAuth.loginParametersForSite(null);
        Assert.assertNotNull(tokenAuthParams);
        Assert.assertEquals(tokenAuthParams.size(), params.size());
        Assert.assertEquals(tokenAuthParams.get("access_token"), params.get("access_token"));
        Assert.assertEquals(tokenAuth.loginPathForSite(null), "/_facebook");
        Assert.assertTrue(tokenAuth.usesCookieBasedLogin());
        Assert.assertNull(tokenAuth.authUserInfo());
    }

    public void testBasicAuthenticator() {
        String username = "username";
        String password = "password";
        BasicAuthenticator basicAuth = new BasicAuthenticator(username, password);

        Assert.assertNull(basicAuth.loginParametersForSite(null));
        Assert.assertTrue(basicAuth.usesCookieBasedLogin());
        Assert.assertEquals(basicAuth.authUserInfo(), username + ":" + password);
    }
}




Java Source Code List

com.couchbase.lite.ApiTest.java
com.couchbase.lite.AttachmentsTest.java
com.couchbase.lite.AuthTest.java
com.couchbase.lite.Base64Test.java
com.couchbase.lite.BlobStoreWriterTest.java
com.couchbase.lite.CRUDOperationsTest.java
com.couchbase.lite.CacheTest.java
com.couchbase.lite.ChangesTest.java
com.couchbase.lite.CollationTest.java
com.couchbase.lite.DatabaseTest.java
com.couchbase.lite.DocumentTest.java
com.couchbase.lite.LitePerfTestCase.java
com.couchbase.lite.LiteTestCase.java
com.couchbase.lite.LiteTestContext.java
com.couchbase.lite.LocalDocsTest.java
com.couchbase.lite.ManagerTest.java
com.couchbase.lite.MiscTest.java
com.couchbase.lite.MultipartReaderTest.java
com.couchbase.lite.RevTreeTest.java
com.couchbase.lite.RevisionsTest.java
com.couchbase.lite.RouterTest.java
com.couchbase.lite.SequenceMapTest.java
com.couchbase.lite.ValidationsTest.java
com.couchbase.lite.ViewsTest.java
com.couchbase.lite.android.AndroidContext.java
com.couchbase.lite.android.AndroidLogger.java
com.couchbase.lite.android.AndroidNetworkReachabilityManager.java
com.couchbase.lite.android.AndroidSQLiteStorageEngineFactory.java
com.couchbase.lite.android.AndroidSQLiteStorageEngine.java
com.couchbase.lite.mockserver.MockBulkDocs.java
com.couchbase.lite.mockserver.MockChangesFeedNoResponse.java
com.couchbase.lite.mockserver.MockChangesFeed.java
com.couchbase.lite.mockserver.MockCheckpointGet.java
com.couchbase.lite.mockserver.MockCheckpointPut.java
com.couchbase.lite.mockserver.MockDispatcher.java
com.couchbase.lite.mockserver.MockDocumentBulkGet.java
com.couchbase.lite.mockserver.MockDocumentGet.java
com.couchbase.lite.mockserver.MockDocumentPut.java
com.couchbase.lite.mockserver.MockFacebookAuthPost.java
com.couchbase.lite.mockserver.MockHelper.java
com.couchbase.lite.mockserver.MockPreloadedPullTarget.java
com.couchbase.lite.mockserver.MockRevsDiff.java
com.couchbase.lite.mockserver.MockSessionGet.java
com.couchbase.lite.mockserver.SmartMockResponse.java
com.couchbase.lite.mockserver.WrappedSmartMockResponse.java
com.couchbase.lite.performance2.Test01_CreateDocs.java
com.couchbase.lite.performance2.Test02_CreateDocsUnoptimizedWay.java
com.couchbase.lite.performance2.Test03_CreateDocsWithAttachments.java
com.couchbase.lite.performance2.Test06_PullReplication.java
com.couchbase.lite.performance2.Test07_PushReplication.java
com.couchbase.lite.performance2.Test08_DocRevisions.java
com.couchbase.lite.performance2.Test09_LoadDB.java
com.couchbase.lite.performance2.Test10_DeleteDB.java
com.couchbase.lite.performance2.Test11_DeleteDocs.java
com.couchbase.lite.performance2.Test12_IndexView.java
com.couchbase.lite.performance2.Test13_QueryView.java
com.couchbase.lite.performance2.Test14_ReduceView.java
com.couchbase.lite.performance2.Test28_KeySizes.java
com.couchbase.lite.performance2.Test29_AllDocQuery.java
com.couchbase.lite.performance2.Test30_LiveQuery.java
com.couchbase.lite.performance2.Test31_CompactDB.java
com.couchbase.lite.performance.Test10_DeleteDB.java
com.couchbase.lite.performance.Test11_DeleteDocs.java
com.couchbase.lite.performance.Test12_IndexView.java
com.couchbase.lite.performance.Test13_QueryView.java
com.couchbase.lite.performance.Test14_ReduceView.java
com.couchbase.lite.performance.Test16_ParallelPushReplication.java
com.couchbase.lite.performance.Test1_CreateDocs.java
com.couchbase.lite.performance.Test2_CreateDocsUnoptimizedWay.java
com.couchbase.lite.performance.Test3_CreateDocsWithAttachments.java
com.couchbase.lite.performance.Test6_PushReplication.java
com.couchbase.lite.performance.Test7_PullReplication.java
com.couchbase.lite.performance.Test8_DocRevisions.java
com.couchbase.lite.performance.Test9_LoadDB.java
com.couchbase.lite.replicator.BulkDownloaderTest.java
com.couchbase.lite.replicator.ChangeTrackerTest.java
com.couchbase.lite.replicator.CustomizableMockHttpClient.java
com.couchbase.lite.replicator.ReplicationTest.java
com.couchbase.lite.replicator.ResponderChain.java
com.couchbase.lite.support.BatcherTest.java
com.couchbase.lite.support.JsonDocumentTest.java
com.couchbase.lite.support.PersistentCookieStoreTest.java
com.couchbase.lite.support.RemoteRequestTest.java
com.couchbase.touchdb.RevCollator.java
com.couchbase.touchdb.TDCollateJSON.java