Skip to content

Commit 24dbf33

Browse files
haverchuckNoyes
andauthored
fix: Throw error when plugin added post-configure (#1150)
Co-authored-by: Noyes <[email protected]>
1 parent 45500e1 commit 24dbf33

12 files changed

+230
-0
lines changed

Amplify/Categories/API/AmplifyAPICategory+APICategory.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ extension AmplifyAPICategory: APICategory {
1919
throw error
2020
}
2121

22+
guard !isConfigured else {
23+
let pluginDescription = String(describing: plugin)
24+
let error = ConfigurationError.amplifyAlreadyConfigured(
25+
"\(pluginDescription) cannot be added after `Amplify.configure()`.",
26+
"Do not add plugins after calling `Amplify.configure()`."
27+
)
28+
throw error
29+
}
30+
2231
plugins[plugin.key] = plugin
2332
}
2433

Amplify/Categories/Analytics/AnalyticsCategory.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ final public class AnalyticsCategory: Category {
5858
throw error
5959
}
6060

61+
guard !isConfigured else {
62+
let pluginDescription = String(describing: plugin)
63+
let error = ConfigurationError.amplifyAlreadyConfigured(
64+
"\(pluginDescription) cannot be added after `Amplify.configure()`.",
65+
"Do not add plugins after calling `Amplify.configure()`."
66+
)
67+
throw error
68+
}
69+
6170
plugins[plugin.key] = plugin
6271
}
6372

Amplify/Categories/Auth/AuthCategory.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ final public class AuthCategory: Category {
5656
throw error
5757
}
5858

59+
guard !isConfigured else {
60+
let pluginDescription = String(describing: plugin)
61+
let error = ConfigurationError.amplifyAlreadyConfigured(
62+
"\(pluginDescription) cannot be added after `Amplify.configure()`.",
63+
"Do not add plugins after calling `Amplify.configure()`."
64+
)
65+
throw error
66+
}
67+
5968
plugins[plugin.key] = plugin
6069
}
6170

Amplify/Categories/DataStore/DataStoreCategory.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ final public class DataStoreCategory: Category {
5959
throw error
6060
}
6161

62+
guard !isConfigured else {
63+
let pluginDescription = String(describing: plugin)
64+
let error = ConfigurationError.amplifyAlreadyConfigured(
65+
"\(pluginDescription) cannot be added after `Amplify.configure()`.",
66+
"Do not add plugins after calling `Amplify.configure()`."
67+
)
68+
throw error
69+
}
70+
6271
plugins[plugin.key] = plugin
6372
}
6473

Amplify/Categories/Predictions/PredictionsCategory.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ final public class PredictionsCategory: Category {
5656
throw error
5757
}
5858

59+
guard !isConfigured else {
60+
let pluginDescription = String(describing: plugin)
61+
let error = ConfigurationError.amplifyAlreadyConfigured(
62+
"\(pluginDescription) cannot be added after `Amplify.configure()`.",
63+
"Do not add plugins after calling `Amplify.configure()`."
64+
)
65+
throw error
66+
}
67+
5968
plugins[plugin.key] = plugin
6069
}
6170

Amplify/Categories/Storage/StorageCategory.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ final public class StorageCategory: Category {
5757
throw error
5858
}
5959

60+
guard !isConfigured else {
61+
let pluginDescription = String(describing: plugin)
62+
let error = ConfigurationError.amplifyAlreadyConfigured(
63+
"\(pluginDescription) cannot be added after `Amplify.configure()`.",
64+
"Do not add plugins after calling `Amplify.configure()`."
65+
)
66+
throw error
67+
}
68+
6069
plugins[plugin.key] = plugin
6170
}
6271

AmplifyTests/CategoryTests/API/APICategoryConfigurationTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,34 @@ class APICategoryConfigurationTests: XCTestCase {
349349

350350
waitForExpectations(timeout: 0.1)
351351
}
352+
353+
/// Test if adding a plugin after configuration throws an error
354+
///
355+
/// - Given: Amplify is configured
356+
/// - When:
357+
/// - Add is called for API category
358+
/// - Then:
359+
/// - Should throw an exception
360+
///
361+
func testAddAfterConfigureThrowsError() throws {
362+
let plugin = MockAPICategoryPlugin()
363+
try Amplify.add(plugin: plugin)
364+
365+
let config = APICategoryConfiguration(
366+
plugins: ["MockAPICategoryPlugin": true]
367+
)
368+
369+
let amplifyConfig = AmplifyConfiguration(api: config)
370+
371+
try Amplify.configure(amplifyConfig)
372+
373+
XCTAssertThrowsError(try Amplify.add(plugin: plugin),
374+
"configure() an already configured plugin should throw") { error in
375+
guard case ConfigurationError.amplifyAlreadyConfigured = error else {
376+
XCTFail("Expected ConfigurationError.amplifyAlreadyConfigured")
377+
return
378+
}
379+
}
380+
381+
}
352382
}

AmplifyTests/CategoryTests/Analytics/AnalyticsCategoryConfigurationTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,33 @@ class AnalyticsCategoryConfigurationTests: XCTestCase {
293293
waitForExpectations(timeout: 0.1)
294294
}
295295

296+
/// Test if adding a plugin after configuration throws an error
297+
///
298+
/// - Given: Amplify is configured
299+
/// - When:
300+
/// - Add is called for Analytics category
301+
/// - Then:
302+
/// - Should throw an exception
303+
///
304+
func testAddAfterConfigureThrowsError() throws {
305+
let plugin = MockAnalyticsCategoryPlugin()
306+
try Amplify.add(plugin: plugin)
307+
308+
let config = AnalyticsCategoryConfiguration(
309+
plugins: ["MockAnalyticsCategoryPlugin": true]
310+
)
311+
312+
let amplifyConfig = AmplifyConfiguration(analytics: config)
313+
314+
try Amplify.configure(amplifyConfig)
315+
316+
XCTAssertThrowsError(try Amplify.add(plugin: plugin),
317+
"configure() an already configured plugin should throw") { error in
318+
guard case ConfigurationError.amplifyAlreadyConfigured = error else {
319+
XCTFail("Expected ConfigurationError.amplifyAlreadyConfigured")
320+
return
321+
}
322+
}
323+
324+
}
296325
}

AmplifyTests/CategoryTests/Auth/AuthCategoryConfigurationTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,34 @@ class AuthCategoryConfigurationTests: XCTestCase {
406406
XCTAssertNotNil(error as? AuthError)
407407
}
408408
}
409+
410+
/// Test if adding a plugin after configuration throws an error
411+
///
412+
/// - Given: Amplify is configured
413+
/// - When:
414+
/// - Add is called for Auth category
415+
/// - Then:
416+
/// - Should throw an exception
417+
///
418+
func testAddAfterConfigureThrowsError() throws {
419+
let plugin = MockAuthCategoryPlugin()
420+
try Amplify.add(plugin: plugin)
421+
422+
let config = AuthCategoryConfiguration(
423+
plugins: ["MockAuthCategoryPlugin": true]
424+
)
425+
426+
let amplifyConfig = AmplifyConfiguration(auth: config)
427+
428+
try Amplify.configure(amplifyConfig)
429+
430+
XCTAssertThrowsError(try Amplify.add(plugin: plugin),
431+
"configure() an already configured plugin should throw") { error in
432+
guard case ConfigurationError.amplifyAlreadyConfigured = error else {
433+
XCTFail("Expected ConfigurationError.amplifyAlreadyConfigured")
434+
return
435+
}
436+
}
437+
438+
}
409439
}

AmplifyTests/CategoryTests/DataStore/DataStoreCategoryConfigurationTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,33 @@ class DataStoreCategoryConfigurationTests: XCTestCase {
312312
waitForExpectations(timeout: 0.1)
313313
}
314314

315+
/// Test if adding a plugin after configuration throws an error
316+
///
317+
/// - Given: Amplify is configured
318+
/// - When:
319+
/// - Add is called for Datastore category
320+
/// - Then:
321+
/// - Should throw an exception
322+
///
323+
func testAddAfterConfigureThrowsError() throws {
324+
let plugin = MockDataStoreCategoryPlugin()
325+
try Amplify.add(plugin: plugin)
326+
327+
let config = DataStoreCategoryConfiguration(
328+
plugins: ["MockDataStoreCategoryPlugin": true]
329+
)
330+
331+
let amplifyConfig = AmplifyConfiguration(dataStore: config)
332+
333+
try Amplify.configure(amplifyConfig)
334+
335+
XCTAssertThrowsError(try Amplify.add(plugin: plugin),
336+
"configure() an already configured plugin should throw") { error in
337+
guard case ConfigurationError.amplifyAlreadyConfigured = error else {
338+
XCTFail("Expected ConfigurationError.amplifyAlreadyConfigured")
339+
return
340+
}
341+
}
342+
343+
}
315344
}

0 commit comments

Comments
 (0)