1+ using System ;
2+ using System . IO ;
3+ using System . Linq ;
4+ using MongoDB . Bson ;
5+ using NUnit . Framework ;
6+ using Realms ;
7+ using Realms . Sync ;
8+ using TaskStatus = dotnet . TaskStatus ;
9+ using Task = dotnet . Task ;
10+
11+ namespace UnitTests
12+ {
13+ public class WorkWithRealm
14+ {
15+ App app ;
16+ ObjectId testTaskId ;
17+ Realms . Sync . User user ;
18+ SyncConfiguration config ;
19+ string myRealmAppId = "tuts-tijya" ;
20+
21+ [ Test ]
22+ public async System . Threading . Tasks . Task LotsaStuff ( )
23+ {
24+ string userEmail = "" ;
25+
26+ // :code-block-start: initialize-realm
27+ // :hide-start:
28+ /*:replace-with:
29+ var myRealmAppId = "<your_app_id>";
30+ var app = App.Create(myRealmAppId);
31+ :code-block-end:*/
32+ // :code-block-start: appConfig
33+ var appConfig = new AppConfiguration ( myRealmAppId )
34+ {
35+ LogLevel = LogLevel . Debug ,
36+ DefaultRequestTimeout = TimeSpan . FromMilliseconds ( 1500 )
37+ } ;
38+
39+ app = App . Create ( appConfig ) ;
40+ //:code-block-end:
41+ // :code-block-start: register-user
42+ await app . EmailPasswordAuth . RegisterUserAsync ( userEmail , "sekrit" ) ;
43+ //:code-block-end:
44+ // :code-block-start: confirm-user
45+ await app . EmailPasswordAuth . ConfirmUserAsync ( "<token>" , "<token-id>" ) ;
46+ //:code-block-end:
47+ // :code-block-start: reset-user-1
48+ await app . EmailPasswordAuth . SendResetPasswordEmailAsync ( userEmail ) ;
49+ //:code-block-end:
50+ string myNewPassword = "" ;
51+ // :code-block-start: reset-user-2
52+ await app . EmailPasswordAuth . ResetPasswordAsync (
53+ myNewPassword , "<token>" , "<token-id>" ) ;
54+ //:code-block-end:
55+ // :code-block-start: reset-user-3
56+ await app . EmailPasswordAuth . CallResetPasswordFunctionAsync (
57+ userEmail , myNewPassword ,
58+ "<security-question-1-answer>" ,
59+ "<security-question-2-answer>" ) ;
60+ //:code-block-end:
61+
62+ }
63+
64+ [ Test ]
65+ public async System . Threading . Tasks . Task APIKeys ( )
66+ {
67+ {
68+ //:code-block-start:apikey-create
69+ var newKey = await user . ApiKeys . CreateAsync ( "someKeyName" ) ;
70+ Console . WriteLine ( $ "I created a key named { newKey . Name } . " +
71+ $ "Is it enabled? { newKey . IsEnabled } ") ;
72+ //:code-block-end:
73+ }
74+ {
75+ //:code-block-start:apikey-fetch
76+ var key = await user . ApiKeys . FetchAsync ( ObjectId . Parse ( "00112233445566778899aabb" ) ) ;
77+ Console . WriteLine ( $ "I fetched the key named { key . Name } . " +
78+ $ "Is it enabled? { key . IsEnabled } ") ;
79+ //:code-block-end:
80+ }
81+ {
82+ //:code-block-start:apikey-fetch-all
83+ var allKeys = await user . ApiKeys . FetchAllAsync ( ) ;
84+ foreach ( var key in allKeys )
85+ {
86+ Console . WriteLine ( $ "I fetched the key named { key . Name } . " +
87+ $ "Is it enabled? { key . IsEnabled } ") ;
88+ }
89+ //:code-block-end:
90+ }
91+ {
92+ //:code-block-start:apikey-enable-disable
93+ var key = await user . ApiKeys . FetchAsync ( ObjectId . Parse ( "00112233445566778899aabb" ) ) ;
94+ if ( ! key . IsEnabled )
95+ {
96+ // enable the key
97+ await user . ApiKeys . EnableAsync ( key . Id ) ;
98+ }
99+ else
100+ {
101+ // disable the key
102+ await user . ApiKeys . DisableAsync ( key . Id ) ;
103+ }
104+ //:code-block-end:
105+ }
106+ {
107+ //:code-block-start:apikey-delete
108+ await user . ApiKeys . DeleteAsync ( ObjectId . Parse ( "00112233445566778899aabb" ) ) ;
109+ //:code-block-end:
110+ }
111+ }
112+ [ Test ]
113+ public async System . Threading . Tasks . Task MultiUser ( )
114+ {
115+ myRealmAppId = "tuts-tijya" ;
116+ var app = App . Create ( myRealmAppId ) ;
117+
118+ {
119+ foreach ( var user in app . AllUsers )
120+ {
121+ await user . LogOutAsync ( ) ;
122+ }
123+ Assert . AreEqual ( 0 , app . AllUsers . Count ( ) ) ;
124+ //:code-block-start:multi-add
125+ var aimee = await app . LogInAsync ( Credentials . EmailPassword (
126+ 127+ Assert . IsTrue ( aimee . Id == app . CurrentUser . Id , "aimee is current user" ) ;
128+
129+ var elvis = await app . LogInAsync ( Credentials . EmailPassword (
130+ 131+ Assert . IsTrue ( elvis . Id == app . CurrentUser . Id , "elvis is current user" ) ;
132+ //:code-block-end:
133+
134+ //:code-block-start:multi-list
135+ foreach ( var user in app . AllUsers )
136+ {
137+ Console . WriteLine ( $ "User { user . Id } is logged on via { user . Provider } ") ;
138+ }
139+ Assert . AreEqual ( 2 , app . AllUsers . Count ( ) ) ;
140+ //:code-block-end:
141+ //:code-block-start:multi-switch
142+ app . SwitchUser ( aimee ) ;
143+ Assert . IsTrue ( aimee . Id == app . CurrentUser . Id , "aimee is current user" ) ;
144+ //:code-block-end:
145+
146+ //:code-block-start:multi-remove
147+ await app . RemoveUserAsync ( elvis ) ;
148+ var noMoreElvis = app . AllUsers . FirstOrDefault ( u => u . Id == elvis . Id ) ;
149+ Assert . IsNull ( noMoreElvis ) ;
150+ Console . WriteLine ( "Elvis has left the application." ) ;
151+ //:code-block-end:
152+ }
153+
154+ return ;
155+ }
156+ }
157+ }
0 commit comments