1
+ using System ;
2
+ using System . Linq ;
3
+ using dotnet ;
4
+ using MongoDB . Bson ;
5
+ using NUnit . Framework ;
6
+ using Realms ;
7
+ using Realms . Sync ;
8
+
9
+ namespace UnitTests
10
+ {
11
+ public class Examples
12
+ {
13
+ App app ;
14
+ ObjectId testTaskId ;
15
+ User user ;
16
+ SyncConfiguration config ;
17
+ const string myRealmAppId = "tuts-tijya" ;
18
+
19
+ [ SetUp ]
20
+ public async System . Threading . Tasks . Task Setup ( )
21
+ {
22
+ // :code-block-start: initialize-realm
23
+ app = App . Create ( myRealmAppId ) ;
24
+ // :code-block-end:
25
+ user = app . LogInAsync ( Credentials . EmailPassword ( "[email protected] " , "foobar" ) ) . Result ;
26
+ config = new SyncConfiguration ( "My Project" , user ) ;
27
+ Realm realm = await Realm . GetInstanceAsync ( config ) ;
28
+ // :code-block-end:
29
+ // :code-block-start: open-synced-realm-sync
30
+ Realm synchronousRealm = Realm . GetInstance ( config ) ;
31
+ // :code-block-end:
32
+ // :code-block-start: create
33
+ RealmTask testTask = new RealmTask
34
+ {
35
+ Name = "Do this thing" ,
36
+ Status = TaskStatus . Open . ToString ( )
37
+ } ;
38
+
39
+ realm . Write ( ( ) =>
40
+ {
41
+ realm . Add ( testTask ) ;
42
+ } ) ;
43
+ // :code-block-end:
44
+ testTaskId = testTask . Id ;
45
+ return ;
46
+ }
47
+
48
+ [ Test ]
49
+ public async System . Threading . Tasks . Task GetsSyncedTasks ( )
50
+ {
51
+ // :code-block-start: anon-login
52
+ User user = app . LogInAsync ( Credentials . Anonymous ( ) ) . Result ;
53
+ // :code-block-end:
54
+ // :code-block-start: config
55
+ config = new SyncConfiguration ( "My Project" , user ) ;
56
+ Realm realm = await Realm . GetInstanceAsync ( config ) ;
57
+ // :code-block-end:
58
+ // :code-block-start: read-all
59
+ var tasks = realm . All < RealmTask > ( ) ;
60
+ // :code-block-end:
61
+ Assert . AreEqual ( 1 , tasks . Count ( ) ) ;
62
+ // :code-block-start: read-some
63
+ tasks = realm . All < RealmTask > ( ) . Where ( t => t . Status == "Open" ) ;
64
+ // :code-block-end:
65
+ Assert . AreEqual ( 1 , tasks . Count ( ) ) ;
66
+ return ;
67
+ }
68
+
69
+ [ Test ]
70
+ public async System . Threading . Tasks . Task ModifiesATask ( )
71
+ {
72
+ config = new SyncConfiguration ( "My Project" , user ) ;
73
+ Realm realm = await Realm . GetInstanceAsync ( config ) ;
74
+ // :code-block-start: modify
75
+ RealmTask t = realm . All < RealmTask > ( )
76
+ . Where ( t => t . Id == testTaskId )
77
+ . FirstOrDefault ( ) ;
78
+
79
+ realm . Write ( ( ) =>
80
+ {
81
+ t . Status = TaskStatus . InProgress . ToString ( ) ;
82
+ } ) ;
83
+
84
+ // :code-block-end:
85
+ var allTasks = realm . All < RealmTask > ( ) . ToList ( ) ;
86
+ Assert . AreEqual ( 1 , allTasks . Count ) ;
87
+ Assert . AreEqual ( TaskStatus . InProgress . ToString ( ) , allTasks . First ( ) . Status ) ;
88
+
89
+ return ;
90
+ }
91
+
92
+ [ Test ]
93
+ public async System . Threading . Tasks . Task LogsOnManyWays ( )
94
+ {
95
+ // :code-block-start: logon_anon
96
+ User anonUser = await app . LogInAsync ( Credentials . Anonymous ( ) ) ;
97
+ // :code-block-end:
98
+ Assert . AreEqual ( UserState . LoggedIn , anonUser . State ) ;
99
+ await anonUser . LogOutAsync ( ) ;
100
+ // :code-block-start: logon_EP
101
+ User emailUser = await app . LogInAsync (
102
+ Credentials . EmailPassword ( "[email protected] " , "shhhItsASektrit!" ) ) ;
103
+ // :code-block-end:
104
+ Assert . AreEqual ( UserState . LoggedIn , emailUser . State ) ;
105
+ await emailUser . LogOutAsync ( ) ;
106
+ var apiKey = "eRECwv1e6gkLEse99XokWOgegzoguEkwmvYvXk08zAucG4kXmZu7TTgV832SwFCv" ;
107
+ // :code-block-start: logon_API
108
+ User apiUser = await app . LogInAsync ( Credentials . ApiKey ( apiKey ) ) ;
109
+ // :code-block-end:
110
+ Assert . AreEqual ( UserState . LoggedIn , apiUser . State ) ;
111
+ await apiUser . LogOutAsync ( ) ;
112
+ // :code-block-start: logon_Function
113
+ var functionParameters = new
114
+ {
115
+ username = "caleb" ,
116
+ password = "shhhItsASektrit!" ,
117
+ IQ = 42 ,
118
+ isCool = false
119
+ } ;
120
+
121
+ User functionUser =
122
+ await app . LogInAsync ( Credentials . Function ( functionParameters ) ) ;
123
+ // :code-block-end:
124
+ Assert . AreEqual ( UserState . LoggedIn , functionUser . State ) ;
125
+ await functionUser . LogOutAsync ( ) ;
126
+ var jwt_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkNhbGViIiwiaWF0IjoxNjAxNjc4ODcyLCJleHAiOjI1MTYyMzkwMjIsImF1ZCI6InR1dHMtdGlqeWEifQ.LHbeSI2FDWrlUVOBxe-rasuFiW-etv2Gu5e3eAa6Y6k" ;
127
+ // :code-block-start: logon_JWT
128
+ User jwtUser =
129
+ await app . LogInAsync ( Credentials . JWT ( jwt_token ) ) ;
130
+ // :code-block-end:
131
+ Assert . AreEqual ( UserState . LoggedIn , jwtUser . State ) ;
132
+ await jwtUser . LogOutAsync ( ) ;
133
+ try
134
+ {
135
+ var facebookToken = "" ;
136
+ // :code-block-start: logon_fb
137
+ User fbUser =
138
+ await app . LogInAsync ( Credentials . Facebook ( facebookToken ) ) ;
139
+ // :code-block-end:
140
+ }
141
+ catch ( Exception e )
142
+ {
143
+ Assert . AreEqual ( "InvalidSession: authentication via 'oauth2-facebook' is unsupported" , e . Message ) ;
144
+ }
145
+ try
146
+ {
147
+ var googleAuthCode = "" ;
148
+ // :code-block-start: logon_google
149
+ User googleUser =
150
+ await app . LogInAsync ( Credentials . Google ( googleAuthCode ) ) ;
151
+ // :code-block-end:
152
+ }
153
+ catch ( Exception e )
154
+ {
155
+ Assert . AreEqual ( "InvalidSession: authentication via 'oauth2-google' is unsupported" , e . Message ) ;
156
+ }
157
+ try
158
+ {
159
+ var appleToken = "" ;
160
+ // :code-block-start: logon_apple
161
+ User appleUser =
162
+ await app . LogInAsync ( Credentials . Apple ( appleToken ) ) ;
163
+ // :code-block-end:
164
+ }
165
+
166
+ catch ( Exception e )
167
+ {
168
+ Assert . AreEqual ( "InvalidSession: authentication via 'oauth2-apple' is unsupported" , e . Message ) ;
169
+ }
170
+ }
171
+
172
+ [ TearDown ]
173
+ public async System . Threading . Tasks . Task TearDown ( )
174
+ {
175
+ config = new SyncConfiguration ( "My Project" , user ) ;
176
+ Realm realm = await Realm . GetInstanceAsync ( config ) ;
177
+ // :code-block-start: delete
178
+ realm . Write ( ( ) =>
179
+ {
180
+ realm . RemoveAll < RealmTask > ( ) ;
181
+ } ) ;
182
+ // :code-block-end:
183
+ // :code-block-start: logout
184
+ await user . LogOutAsync ( ) ;
185
+ // :code-block-end:
186
+ return ;
187
+ }
188
+ }
189
+ }
0 commit comments