Files
fanuzu/app/test/auth_kakao_test.dart
T

140 lines
5.1 KiB
Dart

import 'package:cloud_functions/cloud_functions.dart';
import 'package:fancake/core/firebase/firebase_boot.dart';
import 'package:fancake/features/auth/auth.dart';
import 'package:fancake/features/auth/firebase_account_linker.dart';
import 'package:fancake/features/auth/social_providers.dart';
import 'package:firebase_auth_mocks/firebase_auth_mocks.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:kakao_flutter_sdk_user/kakao_flutter_sdk_user.dart'
as kakao_sdk;
/// 카카오 — SDK 로그인(주입) → 서버 교환(주입) → signInWithCustomToken 계약.
/// 토큰 검증·기존 oidc.kakao 사용자 매핑은 서버 테스트(functions/test/auth_kakao.test.ts)가 본다.
void main() {
setUp(() => FirebaseBoot.debugSetMode(FirebaseMode.live, reason: '카카오 테스트'));
tearDown(
() => FirebaseBoot.debugSetMode(FirebaseMode.mock, reason: '카카오 테스트 끝'),
);
final sent = <Map<String, dynamic>>[];
setUp(sent.clear);
KakaoSignInProvider kakao(
MockFirebaseAuth auth, {
Future<String> Function()? token,
Object? serverError,
String appKey = 'test-key',
}) => KakaoSignInProvider(
accessToken: token ?? () async => 'kakao-access',
exchange: (payload) async {
sent.add(payload);
if (serverError != null) throw serverError;
return {
'token': 'custom-token',
if (payload['link'] == true) 'linked': true,
};
},
firebaseAuth: auth,
nativeAppKey: appKey,
);
test('성공 — 카카오 토큰을 서버에 넘기고 커스텀 토큰으로 로그인한다', () async {
final auth = MockFirebaseAuth(mockUser: MockUser(uid: 'kakao:1'));
final outcome = await kakao(auth).signIn();
expect(sent.single, {'accessToken': 'kakao-access'});
expect((outcome as AuthSucceeded).account.uid, 'kakao:1');
expect(outcome.account.provider, AuthProviderKind.kakao);
expect(auth.currentUser?.uid, 'kakao:1');
});
test('앱 키가 없으면 SDK·서버를 부르지 않고 미설정', () async {
final outcome = await kakao(MockFirebaseAuth(), appKey: '').signIn();
expect(outcome, isA<AuthNotConfigured>());
expect(sent, isEmpty);
});
test('카카오 로그인 취소(CANCELED)는 취소 · 서버를 부르지 않는다', () async {
final outcome = await kakao(
MockFirebaseAuth(),
token: () async => throw PlatformException(code: 'CANCELED'),
).signIn();
expect(outcome, isA<AuthCancelled>());
expect(sent, isEmpty);
});
test('카카오 인가 · 토큰 단계 실패는 로그인 실패(E-AUTH-03) · 서버를 부르지 않는다', () async {
for (final error in [
kakao_sdk.KakaoAuthException(
kakao_sdk.AuthErrorCause.misconfigured,
'KOE',
),
kakao_sdk.KakaoClientException(kakao_sdk.ClientErrorCause.unknown, 'sdk'),
]) {
final outcome = await kakao(
MockFirebaseAuth(),
token: () async => throw error,
).signIn();
expect((outcome as AuthFailed).code, 'E-AUTH-03', reason: '$error');
}
expect(sent, isEmpty);
});
test('서버 오류는 카탈로그 코드로 · 로그인하지 않는다', () async {
final auth = MockFirebaseAuth();
final outcome = await kakao(
auth,
serverError: FirebaseFunctionsException(
code: 'permission-denied',
message: '카카오 인증을 확인하지 못했습니다.',
details: {'code': 'E-AUTH-03'},
),
).signIn();
expect((outcome as AuthFailed).code, 'E-AUTH-03');
expect(auth.currentUser, isNull);
});
group('SNS 연동', () {
test('로그인한 사용자에 link: true 로 붙인다', () async {
final auth = MockFirebaseAuth(
signedIn: true,
mockUser: MockUser(uid: 'u-1'),
);
final linker = FirebaseAccountLinker(
providers: [kakao(auth)],
firebaseAuth: auth,
);
final outcome = await linker.link(AuthProviderKind.kakao);
expect(sent.single, {'accessToken': 'kakao-access', 'link': true});
expect((outcome as AuthSucceeded).account.uid, 'u-1');
});
test('다른 계정에 붙은 카카오면 E-AUTH-08 · 세션 만료면 다시 로그인 안내', () async {
final auth = MockFirebaseAuth(
signedIn: true,
mockUser: MockUser(uid: 'u-1'),
);
final inUse = await kakao(
auth,
serverError: FirebaseFunctionsException(
code: 'already-exists',
message: 'x',
details: {'code': 'E-AUTH-08'},
),
).link(auth.currentUser!);
expect((inUse as AuthFailed).code, 'E-AUTH-08');
expect(inUse.message, '다른 FANUZU 계정에 이미 연결된 소셜 계정입니다.');
final expired = await kakao(
auth,
serverError: FirebaseFunctionsException(
code: 'permission-denied',
message: 'x',
details: {'code': 'E-AUTH-03', 'reason': 'session'},
),
).link(auth.currentUser!);
expect((expired as AuthFailed).message, '보안을 위해 다시 로그인한 뒤 시도해 주세요.');
});
});
}