285 lines
11 KiB
Dart
285 lines
11 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:fancake/core/firebase/firebase_boot.dart';
|
|
import 'package:fancake/features/auth/auth.dart';
|
|
import 'package:fancake/features/auth/auth_repository.dart';
|
|
import 'package:fancake/features/auth/firebase_account_linker.dart';
|
|
import 'package:fancake/features/auth/social_providers.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart' as fb;
|
|
import 'package:firebase_auth_mocks/firebase_auth_mocks.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
/// Firebase 웹 흐름(OIDC) 제공자 — 지금은 Android Apple 이 쓴다(LINE 을 예시 제공자로 둔다). 실 로그인은 못 하고,
|
|
/// Firebase 호출 전후 계약(결과 매핑·취소·연결)만 확인한다.
|
|
void main() {
|
|
// Android 에서 signIn 이 앱 생명주기를 듣는다(untilAbandoned).
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
setUp(() => FirebaseBoot.debugSetMode(FirebaseMode.live, reason: 'OIDC 테스트'));
|
|
tearDown(
|
|
() => FirebaseBoot.debugSetMode(FirebaseMode.mock, reason: 'OIDC 테스트 끝'),
|
|
);
|
|
|
|
OidcSignInProvider line(fb.FirebaseAuth auth) => OidcSignInProvider(
|
|
AuthProviderKind.line,
|
|
providerId: 'oidc.line',
|
|
firebaseAuth: auth,
|
|
);
|
|
|
|
test('기본 목록: 카카오 · LINE 은 SDK + 서버 커스텀 토큰', () {
|
|
final providers = {
|
|
for (final p in AuthRepository().providers) p.provider: p,
|
|
};
|
|
expect(providers[AuthProviderKind.line], isA<LineSignInProvider>());
|
|
expect(providers[AuthProviderKind.kakao], isA<KakaoSignInProvider>());
|
|
});
|
|
|
|
test('Firebase 설정이 없으면 SDK 를 부르지 않고 미설정으로 닫는다', () async {
|
|
FirebaseBoot.debugSetMode(FirebaseMode.mock, reason: '설정 없음');
|
|
final outcome = await line(_ThrowingAuth('should-not-call')).signIn();
|
|
expect(outcome, isA<AuthNotConfigured>());
|
|
});
|
|
|
|
test('성공하면 LINE 계정으로 로그인된다', () async {
|
|
final auth = MockFirebaseAuth(mockUser: MockUser(uid: 'k-1'));
|
|
final outcome = await line(auth).signIn();
|
|
expect(outcome, isA<AuthSucceeded>());
|
|
expect((outcome as AuthSucceeded).account.provider, AuthProviderKind.line);
|
|
expect(outcome.account.uid, 'k-1');
|
|
});
|
|
|
|
test('인가 화면을 닫으면 Android·iOS 철자 모두 취소로 본다', () async {
|
|
for (final code in ['web-context-canceled', 'web-context-cancelled']) {
|
|
final outcome = await line(_ThrowingAuth(code)).signIn();
|
|
expect(outcome, isA<AuthCancelled>(), reason: code);
|
|
}
|
|
});
|
|
|
|
test('콘솔에서 제공자를 안 켰으면 E-AUTH-11', () async {
|
|
final outcome = await line(_ThrowingAuth('operation-not-allowed')).signIn();
|
|
expect((outcome as AuthFailed).code, 'E-AUTH-11');
|
|
});
|
|
|
|
testWidgets('Android: 앱으로 돌아왔는데 결과가 없으면 취소로 풀린다', (tester) async {
|
|
late AuthOutcome outcome;
|
|
line(_HangingAuth()).signIn().then((o) => outcome = o);
|
|
void go(List<AppLifecycleState> states) =>
|
|
states.forEach(tester.binding.handleAppLifecycleStateChanged);
|
|
const inactive = AppLifecycleState.inactive;
|
|
const hidden = AppLifecycleState.hidden;
|
|
go([inactive, hidden, AppLifecycleState.paused]); // Custom Tab 으로 나감
|
|
await tester.pump(const Duration(minutes: 1)); // 브라우저에 오래 있어도 그대로
|
|
go([hidden, inactive, AppLifecycleState.resumed]); // 앱으로 돌아옴
|
|
await tester.pump(const Duration(seconds: 9));
|
|
expect(outcome, isA<AuthCancelled>());
|
|
});
|
|
|
|
group('웹 흐름 중단 뒤 재시도 (10/8 실기기 LINE · R01)', () {
|
|
late List<String> cleared;
|
|
setUp(() {
|
|
cleared = [];
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(const MethodChannel('kr.co.fanuzu.app/auth'), (call) async {
|
|
cleared.add(call.method);
|
|
return null;
|
|
});
|
|
});
|
|
tearDown(() => TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(const MethodChannel('kr.co.fanuzu.app/auth'), null));
|
|
|
|
test('앞선 흐름이 남아 막히면(web-context-already-presented) 정리하고 한 번 다시 열어 로그인된다', () async {
|
|
final auth = _StaleThenOkAuth();
|
|
final outcome = await line(auth).signIn();
|
|
expect(outcome, isA<AuthSucceeded>());
|
|
expect(auth.calls, 2);
|
|
expect(cleared, ['cancelPendingWebSignIn']);
|
|
});
|
|
|
|
test('정리 뒤에도 막히면 소셜 문구로 실패 — 「이메일 또는 비밀번호」가 아니다', () async {
|
|
final outcome = await line(_ThrowingAuth('web-context-already-presented')).signIn();
|
|
expect((outcome as AuthFailed).code, 'E-AUTH-03');
|
|
expect(outcome.message, '로그인이 완료되지 않았습니다. 다시 시도해 주세요.');
|
|
});
|
|
|
|
test('망 오류도 소셜 경로 문구', () async {
|
|
final outcome = await line(_ThrowingAuth('web-network-request-failed')).signIn();
|
|
expect((outcome as AuthFailed).code, 'E-AUTH-03');
|
|
expect(outcome.message, '네트워크 연결을 확인한 뒤 다시 시도해 주세요.');
|
|
});
|
|
|
|
testWidgets('Android: 결과 없이 돌아와 취소로 풀릴 때 Firebase 대기도 정리한다', (tester) async {
|
|
late AuthOutcome outcome;
|
|
line(_HangingAuth()).signIn().then((o) => outcome = o);
|
|
for (final s in [AppLifecycleState.inactive, AppLifecycleState.hidden, AppLifecycleState.paused,
|
|
AppLifecycleState.hidden, AppLifecycleState.inactive, AppLifecycleState.resumed]) {
|
|
tester.binding.handleAppLifecycleStateChanged(s);
|
|
}
|
|
await tester.pump(const Duration(seconds: 9));
|
|
expect(outcome, isA<AuthCancelled>());
|
|
expect(cleared, ['cancelPendingWebSignIn']);
|
|
});
|
|
});
|
|
|
|
group('계정 연결', () {
|
|
test('같은 UID 에 oidc.line 을 붙인다', () async {
|
|
final user = _LinkingUser(linkedEmail: 'fan@fanuzu.co.kr');
|
|
final auth = MockFirebaseAuth(signedIn: true, mockUser: user);
|
|
final linker = FirebaseAccountLinker(
|
|
providers: [line(auth)],
|
|
firebaseAuth: auth,
|
|
);
|
|
|
|
final outcome = await linker.link(AuthProviderKind.line);
|
|
|
|
expect(outcome, isA<AuthSucceeded>());
|
|
expect(linker.linkedAccounts.keys, contains(AuthProviderKind.line));
|
|
});
|
|
|
|
test('이메일이 다르거나 없는 LINE 계정도 붙는다 — 이메일 일치는 소유 확인이 아니다(Q-E8 · R01)', () async {
|
|
for (final email in ['someone@else.com', null]) {
|
|
final user = _LinkingUser(linkedEmail: email);
|
|
final auth = MockFirebaseAuth(signedIn: true, mockUser: user);
|
|
final linker = FirebaseAccountLinker(
|
|
providers: [line(auth)],
|
|
firebaseAuth: auth,
|
|
);
|
|
|
|
final outcome = await linker.link(AuthProviderKind.line);
|
|
|
|
expect(outcome, isA<AuthSucceeded>(), reason: '$email');
|
|
expect(linker.linkedAccounts.keys, contains(AuthProviderKind.line));
|
|
}
|
|
});
|
|
|
|
test('다른 FANUZU 계정에 붙은 LINE 은 Firebase 거부를 E-AUTH-08 로 · 연결되지 않는다', () async {
|
|
final user = _LinkingUser(
|
|
linkedEmail: null,
|
|
error: fb.FirebaseAuthException(code: 'credential-already-in-use'),
|
|
);
|
|
final auth = MockFirebaseAuth(signedIn: true, mockUser: user);
|
|
final linker = FirebaseAccountLinker(
|
|
providers: [line(auth)],
|
|
firebaseAuth: auth,
|
|
);
|
|
|
|
final outcome = await linker.link(AuthProviderKind.line);
|
|
|
|
expect((outcome as AuthFailed).code, 'E-AUTH-08');
|
|
expect(outcome.message, '다른 FANUZU 계정에 이미 연결된 소셜 계정입니다.');
|
|
expect(linker.linkedAccounts.keys, isNot(contains(AuthProviderKind.line)));
|
|
});
|
|
|
|
test('Google · Apple(자격증명 경로)도 이메일이 달라도 붙는다(Q-E8)', () async {
|
|
final auth = MockFirebaseAuth(
|
|
signedIn: true,
|
|
mockUser: MockUser(uid: 'u-1', email: 'fan@fanuzu.co.kr'),
|
|
);
|
|
final outcome = await FirebaseAccountLinker(
|
|
providers: [const _FakeCredential('other@gmail.com')],
|
|
firebaseAuth: auth,
|
|
).link(AuthProviderKind.google);
|
|
expect(outcome, isA<AuthSucceeded>());
|
|
});
|
|
|
|
test('세션이 만료됐으면 다시 로그인 안내(E-AUTH-12)', () async {
|
|
final user = _LinkingUser(
|
|
linkedEmail: null,
|
|
error: fb.FirebaseAuthException(code: 'user-token-expired'),
|
|
);
|
|
final auth = MockFirebaseAuth(signedIn: true, mockUser: user);
|
|
final outcome = await FirebaseAccountLinker(
|
|
providers: [line(auth)],
|
|
firebaseAuth: auth,
|
|
).link(AuthProviderKind.line);
|
|
expect((outcome as AuthFailed).code, 'E-AUTH-12');
|
|
});
|
|
});
|
|
}
|
|
|
|
/// 이메일을 함께 주는 자격증명 제공자(Google 흉내).
|
|
final class _FakeCredential implements SocialAuthProvider, CredentialProvider {
|
|
const _FakeCredential(this.email);
|
|
final String email;
|
|
|
|
@override
|
|
AuthProviderKind get provider => AuthProviderKind.google;
|
|
@override
|
|
bool get isLive => true;
|
|
@override
|
|
List<String> get missingConfig => const [];
|
|
@override
|
|
Future<AuthOutcome> signIn() async => const AuthCancelled();
|
|
@override
|
|
Future<void> signOut() async {}
|
|
@override
|
|
Future<SocialCredential?> obtainCredential() async => (
|
|
credential: fb.GoogleAuthProvider.credential(idToken: 'id'),
|
|
email: email,
|
|
name: null,
|
|
);
|
|
}
|
|
|
|
class _ThrowingAuth extends MockFirebaseAuth {
|
|
_ThrowingAuth(this.code);
|
|
final String code;
|
|
|
|
@override
|
|
Future<fb.UserCredential> signInWithProvider(fb.AuthProvider provider) =>
|
|
throw fb.FirebaseAuthException(code: code);
|
|
}
|
|
|
|
/// 앞선 웹 흐름이 남아 첫 시도가 막히는 Firebase(Android `web-context-already-presented`).
|
|
class _StaleThenOkAuth extends MockFirebaseAuth {
|
|
_StaleThenOkAuth() : super(mockUser: MockUser(uid: 'line-1'));
|
|
var calls = 0;
|
|
|
|
@override
|
|
Future<fb.UserCredential> signInWithProvider(fb.AuthProvider provider) {
|
|
calls += 1;
|
|
if (calls == 1) throw fb.FirebaseAuthException(code: 'web-context-already-presented');
|
|
return super.signInWithProvider(provider);
|
|
}
|
|
}
|
|
|
|
/// 콜백을 잃어 영영 끝나지 않는 Firebase 대기 Task.
|
|
class _HangingAuth extends MockFirebaseAuth {
|
|
@override
|
|
Future<fb.UserCredential> signInWithProvider(fb.AuthProvider provider) =>
|
|
Completer<fb.UserCredential>().future;
|
|
}
|
|
|
|
/// Google 로 가입한 사용자. 연결하면 [linkedEmail] 을 가진 제공자 정보가 붙는다.
|
|
// ignore: must_be_immutable
|
|
class _LinkingUser extends MockUser {
|
|
_LinkingUser({required this.linkedEmail, this.error})
|
|
: super(
|
|
uid: 'u-1',
|
|
email: 'fan@fanuzu.co.kr',
|
|
providerData: [_info('google.com', 'fan@fanuzu.co.kr')],
|
|
);
|
|
|
|
final String? linkedEmail;
|
|
final fb.FirebaseAuthException? error;
|
|
|
|
@override
|
|
Future<fb.UserCredential> linkWithProvider(fb.AuthProvider provider) async {
|
|
if (error case final e?) throw e;
|
|
final result = await super.linkWithProvider(provider);
|
|
providerData
|
|
..removeLast()
|
|
..add(_info(provider.providerId, linkedEmail));
|
|
return result;
|
|
}
|
|
}
|
|
|
|
fb.UserInfo _info(String providerId, String? email) =>
|
|
// ignore: invalid_use_of_protected_member
|
|
fb.UserInfo.fromJson({
|
|
'uid': 'p-$providerId',
|
|
'email': email,
|
|
'providerId': providerId,
|
|
'isAnonymous': false,
|
|
'isEmailVerified': true,
|
|
});
|