aglinxinyuan commented on code in PR #6093:
URL: https://github.com/apache/texera/pull/6093#discussion_r3522748722


##########
common/auth/src/test/scala/org/apache/texera/auth/RequestLoggingFilterSpec.scala:
##########
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.texera.auth
+
+import jakarta.servlet.{DispatcherType, FilterChain}
+import jakarta.servlet.http.{HttpServletRequest, HttpServletResponse}
+import org.eclipse.jetty.servlet.{FilterHolder, ServletContextHandler}
+import org.mockito.ArgumentMatchers.{any, eq => eqTo}
+import org.mockito.Mockito.{mock, verify, when}
+import org.scalatest.flatspec.AnyFlatSpec
+import org.scalatest.matchers.should.Matchers
+
+class RequestLoggingFilterSpec extends AnyFlatSpec with Matchers {
+
+  "RequestLoggingFilter.doFilter" should "delegate to the chain and log the 
request" in {
+    val filter = new RequestLoggingFilter
+    val request = mock(classOf[HttpServletRequest])
+    val response = mock(classOf[HttpServletResponse])
+    val chain = mock(classOf[FilterChain])
+    when(request.getRemoteAddr).thenReturn("1.2.3.4")
+    when(request.getMethod).thenReturn("GET")
+    when(request.getRequestURI).thenReturn("/api/x")
+    when(request.getProtocol).thenReturn("HTTP/1.1")
+    when(response.getStatus).thenReturn(200)
+
+    filter.doFilter(request, response, chain)
+
+    // the chain is always invoked, before any logging
+    verify(chain).doFilter(request, response)
+    // the request/response fields are read to build the INFO log line
+    verify(request).getRemoteAddr
+    verify(request).getMethod
+    verify(request).getRequestURI
+    verify(request).getProtocol
+    verify(response).getStatus
+  }

Review Comment:
   Done — the test now forces the request-log logger to INFO (logback) and uses 
Mockito `inOrder` to assert the chain is invoked before the request fields are 
read for the log line.



##########
common/auth/src/test/scala/org/apache/texera/auth/JwtAuthSpec.scala:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.texera.auth
+
+import org.apache.texera.common.config.AuthConfig
+import org.apache.texera.dao.jooq.generated.enums.UserRoleEnum
+import org.apache.texera.dao.jooq.generated.tables.pojos.User
+import org.jose4j.jwt.NumericDate
+import org.scalatest.flatspec.AnyFlatSpec
+import org.scalatest.matchers.should.Matchers
+
+class JwtAuthSpec extends AnyFlatSpec with Matchers {
+
+  private def buildUser(): User = {
+    val user = new User()
+    user.setUid(42)
+    user.setName("alice")
+    user.setEmail("[email protected]")
+    user.setGoogleId("g-123")
+    user.setGoogleAvatar("avatar-blob")
+    user.setRole(UserRoleEnum.ADMIN)
+    user
+  }
+
+  "JwtAuth.jwtClaims" should "map every User field onto the matching claim" in 
{
+    val claims = JwtAuth.jwtClaims(buildUser(), 7)
+    claims.getSubject shouldBe "alice"
+    claims.getClaimValueAsString("userId") shouldBe "42"
+    claims.getClaimValueAsString("googleId") shouldBe "g-123"
+    claims.getClaimValueAsString("email") shouldBe "[email protected]"
+    claims.getClaimValueAsString("googleAvatar") shouldBe "avatar-blob"
+    claims.getClaimValueAsString("role") shouldBe UserRoleEnum.ADMIN.name
+  }
+
+  it should "derive the expiration from config, ignoring the expireInDays 
argument" in {
+    val claims = JwtAuth.jwtClaims(buildUser(), 7)
+    claims.getExpirationTime should not be null
+    val nowMinutes = NumericDate.now().getValue / 60.0
+    val expMinutes = claims.getExpirationTime.getValue / 60.0
+    // the expireInDays=7 argument is ignored; expiry tracks 
AuthConfig.jwtExpirationMinutes
+    (expMinutes - nowMinutes) shouldBe 
(AuthConfig.jwtExpirationMinutes.toDouble +- 2.0)
+  }

Review Comment:
   Good point — it now calls `jwtClaims` with two very different expireInDays 
values (1 and 100000) and asserts both expiry windows track 
`AuthConfig.jwtExpirationMinutes`, so a config that happens to equal one of 
them can't mask the argument being used.



-- 
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]

Reply via email to