Skip to content

Commit

Permalink
Test external auth providers credentials (#745)
Browse files Browse the repository at this point in the history
Test Facebook credentials
  • Loading branch information
desistefanova authored Aug 12, 2022
1 parent 0c99911 commit f248a3c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
1 change: 0 additions & 1 deletion flutter/realm_flutter/tests/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ dev_dependencies:
flutter_lints: ^2.0.1
build_runner: ^2.1.2
test: ^1.20.1
http: ^0.13.4

flutter:
uses-material-design: true
47 changes: 47 additions & 0 deletions lib/src/cli/atlas_apps/baas_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,53 @@ class BaasClient {
"field_name": "company"
}''');
}

if (confirmationType == null) {
const facebookSecret = "876750ac6d06618b323dee591602897f";
final dynamic createFacebookSecretResult = await _post('groups/$_groupId/apps/$appId/secrets', '{"name":"facebookSecret","value":"$facebookSecret"}');
String facebookClientSecretKeyName = createFacebookSecretResult['name'] as String;
await enableProvider(app, 'oauth2-facebook', config: '''{
"clientId": "1265617494254819"
}''', secretConfig: '''{
"clientSecret": "$facebookClientSecretKeyName"
}''', metadataFelds: '''{
"required": true,
"name": "name"
},
{
"required": true,
"name": "first_name"
},
{
"required": true,
"name": "last_name"
},
{
"required": false,
"name": "email"
},
{
"required": false,
"name": "gender"
},
{
"required": false,
"name": "birthday"
},
{
"required": false,
"name": "min_age"
},
{
"required": false,
"name": "max_age"
},
{
"required": false,
"name": "picture"
}''');
}

print('Creating database db_$name$_appSuffix');

await _createMongoDBService(app, '''{
Expand Down
13 changes: 13 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,16 @@ For the purpose of the tests the token could be generated by the following proce
```
* Once you have the token you can use it to login in the tests with `app.login(Credentials.jwt(token))`. Be sure that the public key configured in the cloud apps is from the correct key pair and corresponds to the private key used for generating this token.

## Manually configure Facebook authentication providers

### Facebook login

1. Login to https://developers.facebook.com/ with account dart.CI.test@gmail.com
2. Go to facebook app `"DartCI"`
3. Go to [Settings/Basic](https://developers.facebook.com/apps/1265617494254819/settings/basic/) and you will find `App ID` and `App secret`
4. Following [Facebook Authentication Instruction](https://www.mongodb.com/docs/atlas/app-services/authentication/facebook/) you have to enable facebook authentication provider in [cloud-qa](https://cloud-qa.mongodb.com/) app. Then set `Client ID = App ID` and `Client Secret = App secret`
5. Go back to https://developers.facebook.com/ in menu [Roles/Test Users](https://developers.facebook.com/apps/1265617494254819/roles/test-users/) and select `"Get a user access token"` option for one of the test users.
This access token could be used for facebook login tests with `Credentials.facebook(accessToken)`.
This token will expire fast. If you need long term token you can receive it from this request
https://graph.facebook.com/v14.0/oauth/access_token?grant_type=fb_exchange_token&client_id={`Client ID`}&client_secret={`Client Secret`}&fb_exchange_token={copied access token from `"Get a user access token"`}
6. Use the recieved token in the test. It will be available for 3.5 days.
19 changes: 19 additions & 0 deletions test/credentials_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,23 @@ Future<void> main([List<String>? args]) async {
await app.logIn(credentials);
}, throws<RealmException>("crypto/rsa: verification error"));
});

///See test/README.md section 'Manually configure Facebook, Google and Apple authentication providers'"
baasTest('Facebook credentials - login', (configuration) async {
final app = App(configuration);
final accessToken =
'EAARZCEokqpOMBAKoIHgaG6bqY6LLseGHcQjYdoPhv9FdB89mkVZBWQFOmZCuVeuRfIa5cMtQANLpZBUQI0n4qb4TZCZCAI3vXZC9Oud2qRiieQDtXqE4abZBQJorcBMzECVfsDlus7hk63zW3XzuFCZAxF4BCdRZBHXlGXIzaHhFHhY72aU1apX0tC';
final credentials = Credentials.facebook(accessToken);
final user = await app.logIn(credentials);
expect(user.state, UserState.loggedIn);
expect(user.provider, AuthProviderType.facebook);
expect(user.profile.name, "Open Graph Test User");
}, skip: "Manual test");

baasTest('Facebook credentials - invalid or expired token', (configuration) async {
final app = App(configuration);
final accessToken = 'invalid or expired token';
final credentials = Credentials.facebook(accessToken);
expect(() async => await app.logIn(credentials), throws<RealmException>("error fetching info from OAuth2 provider"));
});
}

0 comments on commit f248a3c

Please sign in to comment.