From 4535cb36c177599efd9d243f775b53bdea3f1b5f Mon Sep 17 00:00:00 2001 From: zjonn Date: Fri, 27 Aug 2021 02:04:44 +0200 Subject: [PATCH] Add UTs --- lib/providers/auth_provider.dart | 13 +++---- test/auth_provider_test.dart | 66 +++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/lib/providers/auth_provider.dart b/lib/providers/auth_provider.dart index 84d350c..a521f34 100644 --- a/lib/providers/auth_provider.dart +++ b/lib/providers/auth_provider.dart @@ -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 { @@ -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; @@ -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(); } diff --git a/test/auth_provider_test.dart b/test/auth_provider_test.dart index 3928fbe..0c6be10 100644 --- a/test/auth_provider_test.dart +++ b/test/auth_provider_test.dart @@ -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); @@ -66,7 +72,7 @@ void main() { ); test( - 'failingLoginUser', + 'errorLoginUser', () async { when(mockHttpClient.send(any)) .thenAnswer((Invocation a) async => simpleResponse('''{ @@ -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();