@@ -20,22 +20,16 @@ import GTMSessionFetcher
20
20
import XCTest
21
21
22
22
class FacebookTests : TestsBase {
23
- func testSignInWithFacebook( ) {
23
+ func testSignInWithFacebook( ) throws {
24
24
let auth = Auth . auth ( )
25
25
let userInfoDict = self . createFacebookTestingAccount ( )
26
- guard let facebookAccessToken = userInfoDict [ " access_token " ] as! String ? else {
27
- XCTAssertTrue ( false , " Failed to get facebookAccessToken " )
28
- return
29
- }
30
- guard let facebookAccountID = userInfoDict [ " id " ] as! String ? else {
31
- XCTAssertTrue ( false , " Failed to get facebookAccountID " )
32
- return
33
- }
26
+ let facebookAccessToken : String = try XCTUnwrap ( userInfoDict [ " access_token " ] as? String )
27
+ let facebookAccountID : String = try XCTUnwrap ( userInfoDict [ " id " ] as? String )
34
28
let credential = FacebookAuthProvider . credential ( withAccessToken: facebookAccessToken)
35
29
let expectation = self . expectation ( description: " Signing in with Facebook finished. " )
36
30
auth. signIn ( with: credential) { ( result, error) in
37
31
if let error = error {
38
- XCTAssertTrue ( false , " Signing in with Facebook had error: \( error) " )
32
+ XCTFail ( " Signing in with Facebook had error: \( error) " )
39
33
} else {
40
34
XCTAssertEqual ( auth. currentUser? . displayName, Credentials . kFacebookUserName)
41
35
}
@@ -48,26 +42,38 @@ class FacebookTests: TestsBase {
48
42
self . deleteFacebookTestingAccountbyID ( facebookAccountID)
49
43
}
50
44
51
- func testLinkAnonymousAccountToFacebookAccount( ) {
45
+ #if compiler(>=5.5) && canImport(_Concurrency)
46
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
47
+ func testSignInWithFacebookAsync( ) async throws {
48
+ let auth = Auth . auth ( )
49
+ let userInfoDict = try await self . createFacebookTestingAccountAsync ( )
50
+ let facebookAccessToken : String = try XCTUnwrap ( userInfoDict [ " access_token " ] as? String )
51
+ let facebookAccountID : String = try XCTUnwrap ( userInfoDict [ " id " ] as? String )
52
+ let credential = FacebookAuthProvider . credential ( withAccessToken: facebookAccessToken)
53
+
54
+ try await auth. signIn ( with: credential)
55
+ XCTAssertEqual ( auth. currentUser? . displayName, Credentials . kFacebookUserName)
56
+
57
+ // Clean up the created Firebase/Facebook user for future runs.
58
+ try await self . deleteCurrentUserAsync ( )
59
+ try await self . deleteFacebookTestingAccountbyIDAsync ( facebookAccountID)
60
+ }
61
+ #endif
62
+
63
+ func testLinkAnonymousAccountToFacebookAccount( ) throws {
52
64
let auth = Auth . auth ( )
53
65
self . signInAnonymously ( )
54
66
let userInfoDict = self . createFacebookTestingAccount ( )
55
- guard let facebookAccessToken = userInfoDict [ " access_token " ] as! String ? else {
56
- XCTAssertTrue ( false , " Failed to get facebookAccessToken " )
57
- return
58
- }
59
- guard let facebookAccountID = userInfoDict [ " id " ] as! String ? else {
60
- XCTAssertTrue ( false , " Failed to get facebookAccountID " )
61
- return
62
- }
67
+ let facebookAccessToken : String = try XCTUnwrap ( userInfoDict [ " access_token " ] as? String )
68
+ let facebookAccountID : String = try XCTUnwrap ( userInfoDict [ " id " ] as? String )
63
69
let credential = FacebookAuthProvider . credential ( withAccessToken: facebookAccessToken)
64
70
let expectation = self . expectation ( description: " Facebook linking finished. " )
65
71
auth. currentUser? . link ( with: credential, completion: { ( result, error) in
66
72
if let error = error {
67
- XCTAssertTrue ( false , " Link to Firebase error: \( error) " )
73
+ XCTFail ( " Link to Firebase error: \( error) " )
68
74
} else {
69
75
guard let providers = ( auth. currentUser? . providerData) else {
70
- XCTAssertTrue ( false , " Failed to get providers " )
76
+ XCTFail ( " Failed to get providers " )
71
77
return
72
78
}
73
79
XCTAssertEqual ( providers. count, 1 )
@@ -82,6 +88,29 @@ class FacebookTests: TestsBase {
82
88
self . deleteFacebookTestingAccountbyID ( facebookAccountID)
83
89
}
84
90
91
+ #if compiler(>=5.5) && canImport(_Concurrency)
92
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
93
+ func testLinkAnonymousAccountToFacebookAccountAsync( ) async throws {
94
+ let auth = Auth . auth ( )
95
+ try await self . signInAnonymouslyAsync ( )
96
+ let userInfoDict = try await self . createFacebookTestingAccountAsync ( )
97
+ let facebookAccessToken : String = try XCTUnwrap ( userInfoDict [ " access_token " ] as? String )
98
+ let facebookAccountID : String = try XCTUnwrap ( userInfoDict [ " id " ] as? String )
99
+ let credential = FacebookAuthProvider . credential ( withAccessToken: facebookAccessToken)
100
+ try await auth. currentUser? . link ( with: credential)
101
+ guard let providers = ( auth. currentUser? . providerData) else {
102
+ XCTFail ( " Failed to get providers " )
103
+ return
104
+ }
105
+ XCTAssertEqual ( providers. count, 1 )
106
+ XCTAssertEqual ( providers [ 0 ] . providerID, " facebook.com " )
107
+
108
+ // Clean up the created Firebase/Facebook user for future runs.
109
+ try await self . deleteCurrentUserAsync ( )
110
+ try await self . deleteFacebookTestingAccountbyIDAsync ( facebookAccountID)
111
+ }
112
+ #endif
113
+
85
114
///** Creates a Facebook testing account using Facebook Graph API and return a dictionary that
86
115
// * constains "id", "access_token", "login_url", "email" and "password" of the created account.
87
116
// */
@@ -102,22 +131,17 @@ class FacebookTests: TestsBase {
102
131
let error = error as NSError
103
132
if let message = String . init ( data: error. userInfo [ " data " ] as! Data , encoding: . utf8) {
104
133
// May get transient errors here for too many api calls when tests run frequently.
105
- XCTAssertTrue ( false , " Creating Facebook account failed with error: \( message) " )
134
+ XCTFail ( " Creating Facebook account failed with error: \( message) " )
106
135
} else {
107
- XCTAssertTrue ( false , " Creating Facebook account failed with error: \( error) " )
136
+ XCTFail ( " Creating Facebook account failed with error: \( error) " )
108
137
}
109
138
} else {
110
139
do {
111
140
let data = try XCTUnwrap ( data)
112
- guard let userInfo = String . init ( data: data, encoding: . utf8) else {
113
- XCTAssertTrue ( false , " Failed to convert data to string " )
114
- return
115
- }
116
- print ( " The created Facebook testing account info is: \( String ( describing: userInfo) ) " )
117
141
returnValue = try JSONSerialization . jsonObject ( with: data, options: [ ] )
118
142
as! Dictionary < String , Any >
119
143
} catch ( let error) {
120
- XCTAssertTrue ( false , " Failed to unwrap data \( error) " )
144
+ XCTFail ( " Failed to unwrap data \( error) " )
121
145
}
122
146
}
123
147
expectation. fulfill ( )
@@ -126,6 +150,31 @@ class FacebookTests: TestsBase {
126
150
return returnValue
127
151
}
128
152
153
+ #if compiler(>=5.5) && canImport(_Concurrency)
154
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
155
+ ///** Creates a Facebook testing account using Facebook Graph API and return a dictionary that
156
+ // * constains "id", "access_token", "login_url", "email" and "password" of the created account.
157
+ // */
158
+ func createFacebookTestingAccountAsync( ) async throws -> Dictionary < String , Any > {
159
+ let urltoCreateTestUser = " https://graph.facebook.com/ \( Credentials . kFacebookAppID) " +
160
+ " /accounts/test-users "
161
+ let bodyString = " installed=true&name= \( Credentials . kFacebookUserName) " +
162
+ " &permissions=read_stream&access_token= \( Credentials . kFacebookAppAccessToken) "
163
+ let postData = bodyString. data ( using: . utf8)
164
+ let service = GTMSessionFetcherService . init ( )
165
+ let fetcher = service. fetcher ( withURLString: urltoCreateTestUser)
166
+ fetcher. bodyData = postData
167
+ fetcher. setRequestValue ( " text/plain " , forHTTPHeaderField: " Content-Type " )
168
+ let data = try await fetcher. beginFetch ( )
169
+ guard let returnValue = try JSONSerialization . jsonObject ( with: data, options: [ ] )
170
+ as? Dictionary < String , Any > else {
171
+ XCTFail ( " Failed to serialize userInfo as a Dictionary " )
172
+ fatalError ( )
173
+ }
174
+ return returnValue
175
+ }
176
+ #endif
177
+
129
178
//** Delete a Facebook testing account by account Id using Facebook Graph API. */
130
179
func deleteFacebookTestingAccountbyID( _ accountID: String ) {
131
180
let urltoDeleteTestUser = " https://graph.facebook.com/ \( accountID) "
@@ -138,10 +187,24 @@ class FacebookTests: TestsBase {
138
187
let expectation = self . expectation ( description: " Deleting Facebook account finished. " )
139
188
fetcher. beginFetch { ( data, error) in
140
189
if let error = error {
141
- XCTAssertTrue ( false , " Deleting Facebook account failed with error: \( error) " )
190
+ XCTFail ( " Deleting Facebook account failed with error: \( error) " )
142
191
}
143
192
expectation. fulfill ( )
144
193
}
145
194
waitForExpectations ( timeout: TestsBase . kExpectationsTimeout)
146
195
}
196
+ #if compiler(>=5.5) && canImport(_Concurrency)
197
+ @available ( iOS 15 , tvOS 15 , macOS 12 , watchOS 8 , * )
198
+ //** Delete a Facebook testing account by account Id using Facebook Graph API. */
199
+ func deleteFacebookTestingAccountbyIDAsync( _ accountID: String ) async throws {
200
+ let urltoDeleteTestUser = " https://graph.facebook.com/ \( accountID) "
201
+ let bodyString = " method=delete&access_token= \( Credentials . kFacebookAppAccessToken) "
202
+ let postData = bodyString. data ( using: . utf8)
203
+ let service = GTMSessionFetcherService . init ( )
204
+ let fetcher = service. fetcher ( withURLString: urltoDeleteTestUser)
205
+ fetcher. bodyData = postData
206
+ fetcher. setRequestValue ( " text/plain " , forHTTPHeaderField: " Content-Type " )
207
+ try await fetcher. beginFetch ( )
208
+ }
209
+ #endif
147
210
}
0 commit comments