From 3cf0326243587403db37e492b8f1af3f4b988a13 Mon Sep 17 00:00:00 2001 From: Faishal Nelwan <108632813+pesolosep@users.noreply.github.com> Date: Sun, 26 May 2024 12:58:13 +0700 Subject: [PATCH] [REFACTOR] trying to integrate sonar cloud again --- .github/workflows/sonarcloud.yml | 19 ++---- .../utils/JWTUtilsTest.java | 61 ------------------- 2 files changed, 4 insertions(+), 76 deletions(-) delete mode 100644 src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/utils/JWTUtilsTest.java diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index f3e7818..8af2682 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -1,55 +1,44 @@ name: SonarCloud Analysis on: + # Trigger analysis when pushing in master or pull requests, and when creating + # a pull request. push: pull_request: jobs: build: name: Build - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 steps: - - name: Replace placeholders in application-prod.properties - run: | - sed -i 's|${PRODUCTION}|'"${{ secrets.PRODUCTION }}"'|g' src/main/resources/application.properties - sed -i 's|${JDBC_DATABASE_URL}|'"${{ secrets.JDBC_DATABASE_URL }}"'|g' src/main/resources/application-prod.properties - sed -i 's|${JDBC_DATABASE_USERNAME}|'"${{ secrets.JDBC_DATABASE_USERNAME }}"'|g' src/main/resources/application-prod.properties - sed -i 's|${JDBC_DATABASE_PASSWORD}|'"${{ secrets.JDBC_DATABASE_PASSWORD }}"'|g' src/main/resources/application-prod.properties - sed -i 's|${JWT_SECRET}|'"${{ secrets.JWT_SECRET }}"'|g' src/main/resources/application-prod.properties - - name: Check out the Git repository uses: actions/checkout@v4 with: # Shallow clones should be disabled for a better relevancy of analysis fetch-depth: 0 - - name: Set up Java Toolchain uses: actions/setup-java@v4 with: distribution: "temurin" java-version: "21" cache: "gradle" - - name: Cache Gradle packages uses: actions/cache@v4 with: path: ~/.gradle/caches key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }} restore-keys: ${{ runner.os }}-gradle - - name: Cache SonarCloud packages uses: actions/cache@v4 with: path: ~/.sonar/cache key: ${{ runner.os }}-sonar restore-keys: ${{ runner.os }}-sonar - - name: Set gradlew as executable run: chmod +x ./gradlew - - name: Build and analyze run: ./gradlew build jacocoTestReport sonar --info env: # Needed to get some information about the pull request, if any GITHUB_TOKEN: ${{ secrets.GIT_HUB_TOKEN }} # SonarCloud access token should be generated from https://sonarcloud.io/account/security/ - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} \ No newline at end of file diff --git a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/utils/JWTUtilsTest.java b/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/utils/JWTUtilsTest.java deleted file mode 100644 index 41d37a4..0000000 --- a/src/test/java/id/ac/ui/cs/advprog/snackscription_subscriptionbox/utils/JWTUtilsTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package id.ac.ui.cs.advprog.snackscription_subscriptionbox.utils; - -import io.jsonwebtoken.Jwts; -import io.jsonwebtoken.SignatureAlgorithm; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.context.SpringBootTest; - -import javax.crypto.SecretKey; -import javax.crypto.spec.SecretKeySpec; -import java.nio.charset.StandardCharsets; -import java.util.Base64; -import java.util.Date; - -import static org.junit.jupiter.api.Assertions.*; - -@SpringBootTest -class JWTUtilsTest { - - private JWTUtils jwtUtils; - - @Value("${JWT_SECRET}") - private String jwtSecret; - - @BeforeEach - void setUp() { - jwtUtils = new JWTUtils(jwtSecret); - } - - @Test - void testExtractRole() { - String token = createToken("admin", new Date(System.currentTimeMillis() + 1000 * 60 * 60)); // 1 hour validity - String role = jwtUtils.extractRole(token); - assertEquals("admin", role); - } - - @Test - void testIsTokenValid() { - String token = createToken("admin", new Date(System.currentTimeMillis() + 1000 * 60 * 60)); // 1 hour validity - assertTrue(jwtUtils.isTokenValid(token)); - } - - - @Test - void testIsTokenExpiredFalse() { - String token = createToken("admin", new Date(System.currentTimeMillis() + 1000 * 60 * 60)); // 1 hour validity - assertFalse(jwtUtils.isTokenExpired(token)); - } - - private String createToken(String role, Date expiration) { - byte[] keyBytes = Base64.getDecoder().decode(jwtSecret.getBytes(StandardCharsets.UTF_8)); - SecretKey key = new SecretKeySpec(keyBytes, "HmacSHA256"); - - return Jwts.builder() - .claim("role", role) - .setExpiration(expiration) - .signWith(key, SignatureAlgorithm.HS256) - .compact(); - } -}