Philippus commented on code in PR #1928:
URL: https://github.com/apache/pekko-connectors/pull/1928#discussion_r3945192026


##########
google-common/src/test/scala/org/apache/pekko/stream/connectors/google/auth/CredentialsSpec.scala:
##########
@@ -36,6 +39,78 @@ class CredentialsSpec
     super.afterAll()
   }
 
+  private def withJsonFile(contents: String)(test: String => Unit): Unit = {
+    val file = Files.createTempFile("pekko-connectors-credentials", ".json")
+    Files.write(file, contents.getBytes(UTF_8))
+    try test(hoconPath(file))
+    finally Files.deleteIfExists(file): Unit
+  }
+
+  // backslashes are escapes in a HOCON quoted string, and Java accepts 
forward slashes on Windows too
+  private def hoconPath(file: Path): String = 
file.toAbsolutePath.toString.replace('\\', '/')
+
+  private val serviceAccountJson =
+    
"""{"project_id":"a-project","client_email":"[email protected]","private_key":"a-key"}"""
+
+  private val userAccessJson =
+    
"""{"client_id":"an-id","client_secret":"a-secret","refresh_token":"a-token","quota_project_id":"a-project"}"""
+
+  "ServiceAccountCredentials" should {
+
+    "read a service account file" in withJsonFile(serviceAccountJson) { path =>
+      val config = ConfigFactory.parseString(s"""
+           |project-id = ""
+           |client-email = ""
+           |private-key = ""
+           |path = "$path"
+        """.stripMargin)
+
+      ServiceAccountCredentials(config, Set("a-scope")).projectId shouldBe 
"a-project"
+    }
+
+    "propagate the failure, having closed the file, when the file is not a 
service account" in
+    withJsonFile(userAccessJson) { path =>
+      val config = ConfigFactory.parseString(s"""
+           |project-id = ""
+           |client-email = ""
+           |private-key = ""
+           |path = "$path"
+        """.stripMargin)
+
+      // this is the routine case: `gcloud auth application-default login` 
writes a user access file,
+      // and the application-default provider tries the service account parser 
against it first
+      a[Exception] should be thrownBy ServiceAccountCredentials(config, 
Set("a-scope"))

Review Comment:
   ```suggestion
         an[Exception] should be thrownBy ServiceAccountCredentials(config, 
Set("a-scope"))
   ```



##########
google-common/src/test/scala/org/apache/pekko/stream/connectors/google/auth/CredentialsSpec.scala:
##########
@@ -36,6 +39,78 @@ class CredentialsSpec
     super.afterAll()
   }
 
+  private def withJsonFile(contents: String)(test: String => Unit): Unit = {
+    val file = Files.createTempFile("pekko-connectors-credentials", ".json")
+    Files.write(file, contents.getBytes(UTF_8))
+    try test(hoconPath(file))
+    finally Files.deleteIfExists(file): Unit
+  }
+
+  // backslashes are escapes in a HOCON quoted string, and Java accepts 
forward slashes on Windows too
+  private def hoconPath(file: Path): String = 
file.toAbsolutePath.toString.replace('\\', '/')
+
+  private val serviceAccountJson =
+    
"""{"project_id":"a-project","client_email":"[email protected]","private_key":"a-key"}"""
+
+  private val userAccessJson =
+    
"""{"client_id":"an-id","client_secret":"a-secret","refresh_token":"a-token","quota_project_id":"a-project"}"""
+
+  "ServiceAccountCredentials" should {
+
+    "read a service account file" in withJsonFile(serviceAccountJson) { path =>
+      val config = ConfigFactory.parseString(s"""
+           |project-id = ""
+           |client-email = ""
+           |private-key = ""
+           |path = "$path"
+        """.stripMargin)
+
+      ServiceAccountCredentials(config, Set("a-scope")).projectId shouldBe 
"a-project"
+    }
+
+    "propagate the failure, having closed the file, when the file is not a 
service account" in
+    withJsonFile(userAccessJson) { path =>
+      val config = ConfigFactory.parseString(s"""
+           |project-id = ""
+           |client-email = ""
+           |private-key = ""
+           |path = "$path"
+        """.stripMargin)
+
+      // this is the routine case: `gcloud auth application-default login` 
writes a user access file,
+      // and the application-default provider tries the service account parser 
against it first
+      a[Exception] should be thrownBy ServiceAccountCredentials(config, 
Set("a-scope"))
+    }
+  }
+
+  "UserAccessCredentials" should {
+
+    "read a user access file" in withJsonFile(userAccessJson) { path =>
+      val config = ConfigFactory.parseString(s"""
+           |project-id = ""
+           |client-id = ""
+           |client-secret = ""
+           |refresh-token = ""
+           |path = "$path"
+        """.stripMargin)
+
+      UserAccessCredentials(config).projectId shouldBe "a-project"
+    }
+
+    "propagate the failure, having closed the file, when the file is not a 
user access file" in
+    withJsonFile(serviceAccountJson) { path =>
+      val config = ConfigFactory.parseString(s"""
+           |project-id = ""
+           |client-id = ""
+           |client-secret = ""
+           |refresh-token = ""
+           |path = "$path"
+        """.stripMargin)
+
+      a[Exception] should be thrownBy UserAccessCredentials(config)

Review Comment:
   ```suggestion
         an[Exception] should be thrownBy UserAccessCredentials(config)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to