-
Notifications
You must be signed in to change notification settings - Fork 88
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
// :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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hahaha... I guess explains the password 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
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 theRealm realm = await Realm...
repetition.There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 typeRealm
isn't that much more useful than just knowing that I have a variable calledrealm
.