Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

프로덕션 환경의 FCM 설정과 테스트 환경의 FCM 분리 #387

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,34 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("!test")
public class FcmConfiguration {

private static final FirebaseOptions TEST_OPTIONS = FirebaseOptions.builder()
.setCredentials(new MockGoogleCredentials("test-token"))
.setProjectId("test-project")
.build();
private static final FirebaseApp FIREBASE_APP = FirebaseApp.initializeApp(TEST_OPTIONS);

@Value("${fcm.enabled}")
private boolean enabled;

@Value("${fcm.key.path}")
private String FCM_PRIVATE_KEY_PATH;

@Bean
FirebaseMessaging firebaseMessaging() throws IOException {
if (!enabled) {
return FirebaseMessaging.getInstance(FIREBASE_APP);
}

final FileInputStream refreshToken = new FileInputStream(FCM_PRIVATE_KEY_PATH);

final List<FirebaseApp> firebaseApps = FirebaseApp.getApps();

if (firebaseApps.isEmpty()) {
return makeNewInstance(refreshToken);
}

refreshToken.close();
return findExistingInstance(firebaseApps);
}

private FirebaseMessaging makeNewInstance(final InputStream refreshToken) throws IOException {
FirebaseOptions options = FirebaseOptions.builder()
final FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(refreshToken))
.build();
refreshToken.close();

return FirebaseMessaging.getInstance(FirebaseApp.initializeApp(options));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.ddang.ddang.configuration;

import com.ddang.ddang.configuration.fcm.MockGoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("test")
public class FcmConfiguration {

@Bean
public FirebaseMessaging firebaseMessaging() {
final FirebaseApp firebaseApps = findFirebaseApps();

return FirebaseMessaging.getInstance(firebaseApps);
}

private FirebaseApp findFirebaseApps() {
final List<FirebaseApp> apps = FirebaseApp.getApps();

if (!apps.isEmpty()) {
for (final FirebaseApp app : apps) {
if (FirebaseApp.DEFAULT_APP_NAME.equals(app.getName())) {
return app;
}
}
}

final FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(new MockGoogleCredentials("test-token"))
.setProjectId("test-project")
.build();

return FirebaseApp.initializeApp(options);
}
}
3 changes: 3 additions & 0 deletions backend/ddang/src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
spring:
profiles:
default: test

datasource:
url: jdbc:h2:mem:testdb
username: sa
Expand Down