Skip to content

Commit

Permalink
Merge pull request #19 from Zjonn/auth_provider_ut
Browse files Browse the repository at this point in the history
AuthProvider UTs
  • Loading branch information
Zjonn authored Aug 27, 2021
2 parents dfa324c + cd5b2aa commit 363146f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
13 changes: 6 additions & 7 deletions lib/providers/auth_provider.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// https://medium.com/@afegbua/flutter-thursday-13-building-a-user-registration-and-login-process-with-provider-and-external-api-1bb87811fd1d

import 'dart:async';

import 'package:artemis/artemis.dart';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:home_keeper/config/api_url.dart';
import 'package:home_keeper/graphql/graphql_api.dart';

enum Status {
Expand Down Expand Up @@ -35,8 +32,8 @@ class LoginResult {
}

class AuthProvider with ChangeNotifier {
ArtemisClient _client = ArtemisClient(ApiUrl.URL);
FlutterSecureStorage _storage = FlutterSecureStorage();
late final ArtemisClient _client;
late final FlutterSecureStorage _storage;

Status _loggedInStatus = Status.Uninitialized;
Status _registeredInStatus = Status.Uninitialized;
Expand All @@ -45,8 +42,10 @@ class AuthProvider with ChangeNotifier {

Status get registeredInStatus => _registeredInStatus;

AuthProvider(String apiUrl) {
_client = ArtemisClient(apiUrl);
AuthProvider(String apiUrl, [client, storage]) {
_client = client == null ? ArtemisClient(apiUrl) : client;
_storage = storage == null ? FlutterSecureStorage() : storage;

isTokenValid();
}

Expand Down
66 changes: 65 additions & 1 deletion test/auth_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ void main() {
authProvider = AuthProvider.withMocks(artemisClient, mockStorage);
});

test('AuthProviderConstructor', () {
when(mockStorage.read(key: anyNamed('key')))
.thenAnswer((realInvocation) async => null);
AuthProvider("123", artemisClient, mockStorage);
});

test('initialState', () async {
expect(authProvider.loggedInStatus, Status.Uninitialized);
expect(authProvider.registeredInStatus, Status.Uninitialized);
Expand Down Expand Up @@ -66,7 +72,7 @@ void main() {
);

test(
'failingLoginUser',
'errorLoginUser',
() async {
when(mockHttpClient.send(any))
.thenAnswer((Invocation a) async => simpleResponse('''{
Expand All @@ -86,6 +92,64 @@ void main() {
},
);

test('registerUser', () async {
when(mockHttpClient.send(any))
.thenAnswer((Invocation a) async => simpleResponse('''{
"data": {
"register": {
"username": "username",
"errors": []
}
}
} '''));

final res = await authProvider.register(
"username", "email", "password", "password");
expect(res.status, true);
expect(authProvider.registeredInStatus, Status.Registered);
});

test('errorRegisterUser', () async {
when(mockHttpClient.send(any))
.thenAnswer((Invocation a) async => simpleResponse('''{
"errors": [
{
"message": ""
}
],
"data": null
} '''));

final res = await authProvider.register(
"username", "email", "password", "password");
expect(res.status, false);
expect(authProvider.registeredInStatus, Status.NotRegistered);
});

test('dataErrorRegisterUser', () async {
when(mockHttpClient.send(any))
.thenAnswer((Invocation a) async => simpleResponse('''{
"data": {
"register": {
"username": "username",
"errors": [
{
"field": "username",
"messages": [
"A user with that username already exists."
]
}
]
}
}
} '''));

final res = await authProvider.register(
"username", "email", "password", "password");
expect(res.status, false);
expect(authProvider.registeredInStatus, Status.NotRegistered);
});

test('logout', () async {
await authProvider.logout();

Expand Down

0 comments on commit 363146f

Please sign in to comment.