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

add auth page with code snippets #512

Merged
merged 3 commits into from
Oct 7, 2020
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
189 changes: 189 additions & 0 deletions examples/dotnet/Examples/Examples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using System;
using System.Linq;
using dotnet;
using MongoDB.Bson;
using NUnit.Framework;
using Realms;
using Realms.Sync;

namespace UnitTests
{
public class Examples
{
App app;
ObjectId testTaskId;
User user;
SyncConfiguration config;
const string myRealmAppId = "tuts-tijya";

[SetUp]
public async System.Threading.Tasks.Task Setup()
{
// :code-block-start: initialize-realm
app = App.Create(myRealmAppId);
// :code-block-end:
user = app.LogInAsync(Credentials.EmailPassword("[email protected]", "foobar")).Result;
config = new SyncConfiguration("My Project", user);
Realm realm = await Realm.GetInstanceAsync(config);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably move realm to a class-level variable as well to avoid the Realm realm = await Realm... repetition.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these code snippets live "alone" within the docs, and the user won't be seeing this unit test file as a whole, I think it's useful to have the Realm there to be clear what we are building.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but it's in the variable name as well - i.e. knowing that I have a variable called realm of type Realm isn't that much more useful than just knowing that I have a variable called realm.

// :code-block-end:
// :code-block-start: open-synced-realm-sync
Realm synchronousRealm = Realm.GetInstance(config);
// :code-block-end:
// :code-block-start: create
RealmTask testTask = new RealmTask
{
Name = "Do this thing",
Status = TaskStatus.Open.ToString()
};

realm.Write(() =>
{
realm.Add(testTask);
});
// :code-block-end:
testTaskId = testTask.Id;
return;
}

[Test]
public async System.Threading.Tasks.Task GetsSyncedTasks()
{
// :code-block-start: anon-login
User user = app.LogInAsync(Credentials.Anonymous()).Result;
// :code-block-end:
// :code-block-start: config
config = new SyncConfiguration("My Project", user);
Realm realm = await Realm.GetInstanceAsync(config);
// :code-block-end:
// :code-block-start: read-all
var tasks = realm.All<RealmTask>();
// :code-block-end:
Assert.AreEqual(1, tasks.Count());
// :code-block-start: read-some
tasks = realm.All<RealmTask>().Where(t => t.Status == "Open");
// :code-block-end:
Assert.AreEqual(1, tasks.Count());
return;
}

[Test]
public async System.Threading.Tasks.Task ModifiesATask()
{
config = new SyncConfiguration("My Project", user);
Realm realm = await Realm.GetInstanceAsync(config);
// :code-block-start: modify
RealmTask t = realm.All<RealmTask>()
.Where(t => t.Id == testTaskId)
.FirstOrDefault();

realm.Write(() =>
{
t.Status = TaskStatus.InProgress.ToString();
});

// :code-block-end:
var allTasks = realm.All<RealmTask>().ToList();
Assert.AreEqual(1, allTasks.Count);
Assert.AreEqual(TaskStatus.InProgress.ToString(), allTasks.First().Status);

return;
}

[Test]
public async System.Threading.Tasks.Task LogsOnManyWays()
{
// :code-block-start: logon_anon
User anonUser = await app.LogInAsync(Credentials.Anonymous());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should prefix the variable name with the provider type - in the iOS/Android docs, we're using user for all the examples. Since that will result in compilation errors here, you can add a scope around each snippet:

public async System.Threading.Tasks.Task LogsOnManyWays()
{
    {
        // :code-block-start: logon_anon
        var user = await app.LogInAsync(CredentialAnonymous());
        // :code-block-end:
        Assert.AreEqual(UserState.LoggedIn, user.State);
        await user.LogOutAsync();
    }

    {
        // :code-block-start: logon_EP
        var user = await app.LogInAsync(
            Credentials.EmailPassword("[email protected]", "shhhItsASektrit!"));
        // :code-block-end:
        Assert.AreEqual(UserState.LoggedIn, user.State);
        await user.LogOutAsync();
    }
    // ...
}

// :code-block-end:
Assert.AreEqual(UserState.LoggedIn, anonUser.State);
await anonUser.LogOutAsync();
// :code-block-start: logon_EP
User emailUser = await app.LogInAsync(
Credentials.EmailPassword("[email protected]", "shhhItsASektrit!"));
// :code-block-end:
Assert.AreEqual(UserState.LoggedIn, emailUser.State);
await emailUser.LogOutAsync();
var apiKey = "eRECwv1e6gkLEse99XokWOgegzoguEkwmvYvXk08zAucG4kXmZu7TTgV832SwFCv";
// :code-block-start: logon_API
User apiUser = await app.LogInAsync(Credentials.ApiKey(apiKey));
// :code-block-end:
Assert.AreEqual(UserState.LoggedIn, apiUser.State);
await apiUser.LogOutAsync();
// :code-block-start: logon_Function
var functionParameters = new
{
username= "caleb",
password = "shhhItsASektrit!",
IQ = 42,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahaha... I guess explains the password 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell I was getting punchy?

isCool = false
};

User functionUser =
await app.LogInAsync(Credentials.Function(functionParameters));
// :code-block-end:
Assert.AreEqual(UserState.LoggedIn, functionUser.State);
await functionUser.LogOutAsync();
var jwt_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkNhbGViIiwiaWF0IjoxNjAxNjc4ODcyLCJleHAiOjI1MTYyMzkwMjIsImF1ZCI6InR1dHMtdGlqeWEifQ.LHbeSI2FDWrlUVOBxe-rasuFiW-etv2Gu5e3eAa6Y6k";
// :code-block-start: logon_JWT
User jwtUser =
await app.LogInAsync(Credentials.JWT(jwt_token));
// :code-block-end:
Assert.AreEqual(UserState.LoggedIn, jwtUser.State);
await jwtUser.LogOutAsync();
try
{
var facebookToken = "";
// :code-block-start: logon_fb
User fbUser =
await app.LogInAsync(Credentials.Facebook(facebookToken));
// :code-block-end:
}
catch (Exception e)
{
Assert.AreEqual("InvalidSession: authentication via 'oauth2-facebook' is unsupported", e.Message);
}
try
{
var googleAuthCode = "";
// :code-block-start: logon_google
User googleUser =
await app.LogInAsync(Credentials.Google(googleAuthCode));
// :code-block-end:
}
catch (Exception e)
{
Assert.AreEqual("InvalidSession: authentication via 'oauth2-google' is unsupported", e.Message);
}
try
{
var appleToken = "";
// :code-block-start: logon_apple
User appleUser =
await app.LogInAsync(Credentials.Apple(appleToken));
// :code-block-end:
}

catch (Exception e)
{
Assert.AreEqual("InvalidSession: authentication via 'oauth2-apple' is unsupported", e.Message);
}
}

[TearDown]
public async System.Threading.Tasks.Task TearDown()
{
config = new SyncConfiguration("My Project", user);
Realm realm = await Realm.GetInstanceAsync(config);
// :code-block-start: delete
realm.Write(() =>
{
realm.RemoveAll<RealmTask>();
});
// :code-block-end:
// :code-block-start: logout
await user.LogOutAsync();
// :code-block-end:
return;
}
}
}
5 changes: 3 additions & 2 deletions examples/dotnet/Examples/Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="Realm.Fody" Version="5.0.1-PR-2041-31">
<PackageReference Include="MongoDB.Bson" Version="2.11.2" />
<PackageReference Include="Realm.Fody" Version="10.0.0-alpha.39">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Realm" Version="5.0.1-PR-2041-31" />
<PackageReference Include="Realm" Version="10.0.0-alpha.39" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace dotnet
{
public class Project : RealmObject
public class RealmProject : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Realms;
namespace dotnet
{
public class Task : RealmObject
public class RealmTask : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
Expand All @@ -13,7 +13,7 @@ public class Task : RealmObject
public string Partition { get; set; }

[MapTo("assignee")]
public User Assignee { get; set; }
public RealmUser Assignee { get; set; }

[MapTo("name")]
[Required]
Expand All @@ -23,7 +23,7 @@ public class Task : RealmObject
[Required]
public string Status { get; set; }

public Task()
public RealmTask()
{
this.Id = ObjectId.GenerateNewId();
}
Expand Down
105 changes: 0 additions & 105 deletions examples/dotnet/Examples/RealmTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace dotnet
{
public class User : RealmObject
public class RealmUser : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
Expand Down
26 changes: 13 additions & 13 deletions examples/dotnet/dotnet.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{C97249A2-33A1-457A-AAEC-389B62984885}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -13,17 +13,17 @@ Global
Release|iPhone = Release|iPhone
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|Any CPU.Build.0 = Release|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Debug|iPhone.Build.0 = Debug|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhone.ActiveCfg = Release|Any CPU
{C97249A2-33A1-457A-AAEC-389B62984885}.Release|iPhone.Build.0 = Release|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|Any CPU.Build.0 = Release|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Debug|iPhone.Build.0 = Debug|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|iPhone.ActiveCfg = Release|Any CPU
{C7CFE106-78AB-4B53-A1F1-1008EAD4FA5C}.Release|iPhone.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Loading