diff --git a/.github/composite/build-image/internal/leetcode-auth-service/action.yml b/.github/composite/build-image/internal/leetcode-auth-service/action.yml new file mode 100644 index 000000000..bd0dbdb77 --- /dev/null +++ b/.github/composite/build-image/internal/leetcode-auth-service/action.yml @@ -0,0 +1,45 @@ +name: "Build & Upload Docker Image for codebloom-leetcode-auth-service" +description: "Build & (optionally) upload Docker Image to Docker Registry" + +inputs: + ENVIRONMENT: + description: "Deployment environment (staging or production)." + required: false + default: production + SECRETS: + description: "Pass toJSON(secrets) from the calling workflow to expose all secrets as env vars." + required: false + DOCKER_UPLOAD: + description: "Boolean indicating whether the image should be uploaded to Docker registry or not." + required: false + default: true + ARCH: + description: "Target architecture to build natively (amd64 or arm64). Each arch pushes to its own Docker repository (codebloom-leetcode-auth-service / codebloom-leetcode-auth-service-arm)." + required: false + default: amd64 + +outputs: + tag: + description: "Built Docker image tag (git SHA)" + value: ${{ steps.build-image.outputs.tag }} + +runs: + using: "composite" + steps: + - name: Setup CI + uses: tahminator/pipeline/actions/setup@a424f92d486342e1255fcdf377a7569eb230aabe + with: + LINT_CI: true + SECRETS: ${{ inputs.SECRETS }} + + - name: Run script + id: build-image + shell: bash + run: | + bun run .github/scripts/build-image \ + --docker-upload="${{ inputs.DOCKER_UPLOAD }}" \ + --getGhaOutput=true \ + --type="leetcode-auth-service" \ + --arch="${{ inputs.ARCH }}" \ + --environment="${{ inputs.ENVIRONMENT }}" \ + --prId="${{ github.event.pull_request.number }}" diff --git a/.github/composite/redeploy/internal/leetcode-auth-service/action.yml b/.github/composite/redeploy/internal/leetcode-auth-service/action.yml new file mode 100644 index 000000000..7792dc22d --- /dev/null +++ b/.github/composite/redeploy/internal/leetcode-auth-service/action.yml @@ -0,0 +1,31 @@ +name: "Re-Deploy codebloom-leetcode-auth-service to K8s" +description: "Deploy the leetcode auth service to K8s by opening and merging a GitOps image-tag PR." + +inputs: + SECRETS: + description: "Pass toJSON(secrets) from the calling workflow to expose all secrets as env vars." + required: false + ENVIRONMENT: + description: '"staging" or "production"' + required: false + default: production + SHA: + description: "Commit SHA of the built image. Defaults to GITHUB_SHA when omitted." + required: false + +runs: + using: "composite" + steps: + - name: Setup CI + uses: tahminator/pipeline/actions/setup@a424f92d486342e1255fcdf377a7569eb230aabe + with: + LINT_CI: true + SECRETS: ${{ inputs.SECRETS }} + + - name: Run script + shell: bash + run: | + bun run .github/scripts/redeploy \ + --environment="${{ inputs.ENVIRONMENT }}" \ + --sha="${{ inputs.SHA }}" \ + --type="leetcode-auth-service" diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 80f193a86..b26e751f6 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -35,10 +35,16 @@ updates: patterns: ["*"] cooldown: { default-days: 7 } multi-ecosystem-group: main + - package-ecosystem: cargo + directory: /internal/leetcode-auth-service + patterns: ["*"] + cooldown: { default-days: 7 } + multi-ecosystem-group: main - package-ecosystem: docker directories: - /infra - /internal/leetcode-auth-stealer-service + - /internal/leetcode-auth-service patterns: ["*"] cooldown: { default-days: 7 } multi-ecosystem-group: main diff --git a/.github/scripts/build-image/index.ts b/.github/scripts/build-image/index.ts index 6846c72b8..ab86ce79f 100644 --- a/.github/scripts/build-image/index.ts +++ b/.github/scripts/build-image/index.ts @@ -35,7 +35,12 @@ const { environment, dockerUpload, getGhaOutput, githubOutputFile, type, arch, p default: process.env.GITHUB_OUTPUT, }) .option("type", { - choices: ["standup-bot", "web", "leetcode-auth-stealer-service"] satisfies Type[], + choices: [ + "standup-bot", + "web", + "leetcode-auth-stealer-service", + "leetcode-auth-service", + ] satisfies Type[], describe: "Type to build", demandOption: true, default: "web" as Type, @@ -112,7 +117,9 @@ async function main() { const buildArgs = { ...(type === "web" ? { COMMIT_SHA: gitSha } : {}), - ...(environment === "staging" && type !== "leetcode-auth-stealer-service" + ...(environment === "staging" && + type !== "leetcode-auth-stealer-service" && + type !== "leetcode-auth-service" ? { VITE_STAGING: true, } @@ -124,6 +131,10 @@ async function main() { if (type === "leetcode-auth-stealer-service") { process.chdir("internal/leetcode-auth-stealer-service"); } + // build.rs also compiles LASS's proto, so the build context is internal/ + if (type === "leetcode-auth-service") { + process.chdir("internal"); + } await dockerClient.buildImage({ dockerRepository, dockerFileLocation: @@ -131,7 +142,9 @@ async function main() { ? "infra/Dockerfile" : type === "standup-bot" ? "internal/standup-bot/Dockerfile" - : "Dockerfile", + : type === "leetcode-auth-service" + ? "leetcode-auth-service/Dockerfile" + : "Dockerfile", tags, shouldUpload: dockerUpload, buildArgs, @@ -161,6 +174,7 @@ async function main() { prId, owner: "Patina-Network", repository: "codebloom", + key: `build-${dockerRepository}`, message: `The image has been uploaded to https://hub.docker.com/r/patinanetwork/${dockerRepository}/tags under the following tags: ${tags.map((t) => `- \`${dockerRepository}:${t}\``).join("\n")} diff --git a/.github/scripts/bun.lock b/.github/scripts/bun.lock index f6b9d5d83..d2cf9ab2e 100644 --- a/.github/scripts/bun.lock +++ b/.github/scripts/bun.lock @@ -7,7 +7,7 @@ "dependencies": { "@actions/artifact": "^5.0.2", "@octokit/rest": "^22.0.1", - "@tahminator/pipeline": "1.0.97", + "@tahminator/pipeline": "^1.0.101", "@types/yargs": "^17.0.35", "bun": "^1.3.6", "discord.js": "^14.25.1", @@ -380,7 +380,7 @@ "@sigstore/verify": ["@sigstore/verify@3.1.1", "", { "dependencies": { "@sigstore/bundle": "^4.0.0", "@sigstore/core": "^3.2.1", "@sigstore/protobuf-specs": "^0.5.0" } }, "sha512-qv7+G3J2cc6wwFj3yKvXOamzqhMwSk1ogPGmhpS8iXllcPrJaIIBA+4HbttlHVu1pqWTdmaCH/WE7UOC51kdoA=="], - "@tahminator/pipeline": ["@tahminator/pipeline@1.0.97", "", { "dependencies": { "@actions/artifact": "^6.2.1", "@octokit/auth-app": "^8.2.0", "@octokit/rest": "^22.0.1", "@pulumi/pulumi": "^3.258.0", "ansi-to-html": "^0.7.2", "fast-xml-builder": "^1.1.5", "fast-xml-parser": "^5.7.2", "neverthrow": "^8.2.0", "oxlint-tsgolint": "^7.0.2003", "semver": "^7.7.4", "xml-trueformat": "^0.9.5", "yaml": "^2.8.2", "yargs": "^18.0.0", "zod": "^4.3.6" }, "peerDependencies": { "typescript": "^7" } }, "sha512-8i20oL+H72CplvWT4gBB2G79KpbKQ9y5yRLLkjuQXjeXLWzmMAajbKEEC3Ja2J29DrNvYOYLnKaMIFy38bMngg=="], + "@tahminator/pipeline": ["@tahminator/pipeline@1.0.101", "", { "dependencies": { "@actions/artifact": "^6.2.1", "@octokit/auth-app": "^8.2.0", "@octokit/rest": "^22.0.1", "@pulumi/pulumi": "^3.258.0", "ansi-to-html": "^0.7.2", "fast-xml-builder": "^1.1.5", "fast-xml-parser": "^5.7.2", "neverthrow": "^8.2.0", "oxlint-tsgolint": "^7.0.2003", "semver": "^7.7.4", "xml-trueformat": "^0.9.5", "yaml": "^2.8.2", "yargs": "^18.0.0", "zod": "^4.3.6" }, "peerDependencies": { "typescript": "^7" } }, "sha512-0Xe2/wSBi2rhxmnSToe+j7TWU33rk4v+Sfvb2celTaIsUk48knBJZZLudGvnxu7RCdkaDpOkhxQE4J5SUICmew=="], "@tufjs/canonical-json": ["@tufjs/canonical-json@2.0.0", "", {}, "sha512-yVtV8zsdo8qFHe+/3kw81dSLyF7D576A5cCFCi4X7B39tWT7SekaEFUnvnWJHz+9qO7qJTah1JbrDjWKqFtdWA=="], diff --git a/.github/scripts/package.json b/.github/scripts/package.json index c1fe80c7a..d71ebe0d7 100644 --- a/.github/scripts/package.json +++ b/.github/scripts/package.json @@ -18,7 +18,7 @@ "dependencies": { "@actions/artifact": "^5.0.2", "@octokit/rest": "^22.0.1", - "@tahminator/pipeline": "1.0.97", + "@tahminator/pipeline": "^1.0.101", "@types/yargs": "^17.0.35", "bun": "^1.3.6", "discord.js": "^14.25.1", diff --git a/.github/scripts/redeploy/index.ts b/.github/scripts/redeploy/index.ts index b8ff404e0..989d34d09 100644 --- a/.github/scripts/redeploy/index.ts +++ b/.github/scripts/redeploy/index.ts @@ -18,7 +18,12 @@ const { environment, sha, type } = await yargs(hideBin(process.argv)) default: "", }) .option("type", { - choices: ["standup-bot", "web", "leetcode-auth-stealer-service"] satisfies Type[], + choices: [ + "standup-bot", + "web", + "leetcode-auth-stealer-service", + "leetcode-auth-service", + ] satisfies Type[], describe: "Type to build", demandOption: true, default: "web" as Type, diff --git a/.github/scripts/test/run-backend-tests.ts b/.github/scripts/test/run-backend-tests.ts index 9ea0372b0..2c68befc9 100644 --- a/.github/scripts/test/run-backend-tests.ts +++ b/.github/scripts/test/run-backend-tests.ts @@ -38,8 +38,6 @@ async function main() { await $`./mvnw -B install -D skipTests --no-transfer-progress`; - await $`./mvnw -B -ntp exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install --with-deps"`; - await $`cd email && pnpm i --frozen-lockfile && ./email.sh && cd ..`; await $$`./mvnw -B -ntp clean verify -Dspring.profiles.active=ci`; diff --git a/.github/scripts/types.ts b/.github/scripts/types.ts index 1208f608c..ba509e400 100644 --- a/.github/scripts/types.ts +++ b/.github/scripts/types.ts @@ -2,4 +2,8 @@ export type Environment = "staging" | "production"; export type Location = "backend" | "frontend"; -export type Type = "web" | "standup-bot" | "leetcode-auth-stealer-service"; +export type Type = + | "web" + | "standup-bot" + | "leetcode-auth-stealer-service" + | "leetcode-auth-service"; diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index a2023a54c..dbfc2dbbb 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -179,6 +179,54 @@ jobs: ENVIRONMENT: staging SHA: ${{ github.sha }} + buildStagingLasImage: + name: Build staging image for codebloom-leetcode-auth-service (${{ matrix.arch }}) + runs-on: ${{ matrix.runner }} + + if: github.event_name == 'pull_request' + + strategy: + matrix: + include: + - arch: amd64 + runner: ubuntu-26.04 + - arch: arm64 + runner: ubuntu-26.04-arm + + steps: + - name: Checkout repository + uses: actions/checkout@v7 + with: + ref: ${{ github.sha }} + + - name: Run workflow + uses: ./.github/composite/build-image/internal/leetcode-auth-service + with: + SECRETS: ${{ toJSON(secrets) }} + ENVIRONMENT: staging + DOCKER_UPLOAD: "true" + ARCH: ${{ matrix.arch }} + + deployStagingLas: + name: Deploy staging codebloom-leetcode-auth-service to K8s + runs-on: ubuntu-26.04 + needs: buildStagingLasImage + + if: github.event_name == 'pull_request' + + steps: + - name: Checkout repository + uses: actions/checkout@v7 + with: + ref: ${{ github.sha }} + + - name: Run workflow + uses: ./.github/composite/redeploy/internal/leetcode-auth-service + with: + SECRETS: ${{ toJSON(secrets) }} + ENVIRONMENT: staging + SHA: ${{ github.sha }} + validateDBSchema: name: Validate DB Schema on Prod DB runs-on: ubuntu-26.04 @@ -294,6 +342,54 @@ jobs: ENVIRONMENT: production SHA: ${{ github.sha }} + buildLasImage: + name: Build codebloom-leetcode-auth-service Docker Image & Upload to Registry (${{ matrix.arch }}) + runs-on: ${{ matrix.runner }} + + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + + strategy: + matrix: + include: + - arch: amd64 + runner: ubuntu-26.04 + - arch: arm64 + runner: ubuntu-26.04-arm + + steps: + - name: Checkout repository + uses: actions/checkout@v7 + with: + ref: ${{ github.sha }} + + - name: Run workflow + uses: ./.github/composite/build-image/internal/leetcode-auth-service + with: + SECRETS: ${{ toJSON(secrets) }} + ENVIRONMENT: production + DOCKER_UPLOAD: "true" + ARCH: ${{ matrix.arch }} + + deployLas: + name: Deploy codebloom-leetcode-auth-service to K8s + runs-on: ubuntu-26.04 + needs: buildLasImage + + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + + steps: + - name: Checkout repository + uses: actions/checkout@v7 + with: + ref: ${{ github.sha }} + + - name: Run workflow + uses: ./.github/composite/redeploy/internal/leetcode-auth-service + with: + SECRETS: ${{ toJSON(secrets) }} + ENVIRONMENT: production + SHA: ${{ github.sha }} + deploy: name: deploy codebloom to K8S runs-on: ubuntu-26.04 diff --git a/example.env b/example.env index f869170e0..6410002fa 100644 --- a/example.env +++ b/example.env @@ -26,35 +26,8 @@ DISCORD_REPORTING_CHANNEL_ID= DISCORD_REPORTING_LOG_GUILD_ID= DISCORD_REPORTING_LOG_CHANNEL_ID= -# unset will point LASS (Leetcode Auth Stealer Service) to localhost:50051 -# uncomment these to use staging LASS (must be on VPN). -# GRPC_LASS_HOST=stg.codebloom-leetcode-auth-stealer-service.vpn.patinanetwork.org:50051 -# GRPC_LASS_TLS=true - -# default is 2m, can change it if necessary for local dev. -GRPC_LASS_TIMEOUT= - -# optional for auth. -# GitHub username and password for the auth token stealer. This GitHub account must -# already have logged into leetcode.com once before. -GH_USERNAME= -GH_PASSWORD= - -# optional for auth, can skip -# You must attach the email used for the Github account, as the GitHub page may require a 2FA code. -# As of right now, you must use port 143 as it is configured to use unencrypted IMAP. -# -# TODO - Replace 143 with 993 to be secure. -GH_EMAIL_HOST= -GH_EMAIL_PORT=143 -GH_EMAIL_TYPE=IMAP -GH_EMAIL_USERNAME= -GH_EMAIL_PASSWORD= - # required -# This email will be used to send school verification messages. This is separated from Github since -# we only use one official email for sending these messages, but have a variety of GitHub accounts -# between dev, staging, and prod. You must use TLS (port 587) as that is what it is configured for. +# This official email sends school verification messages using TLS (port 587). CODEBLOOM_EMAIL_HOST= CODEBLOOM_EMAIL_PORT=587 CODEBLOOM_EMAIL_TYPE=SMTP @@ -77,11 +50,18 @@ SECRET_KEY= SERVER_URL=http://localhost:5173 # read by leetcode-auth-service + +# leetcode.com username/email and password. LASS logs in with these directly on +# leetcode.com's login form, so they must be LeetCode credentials, not GitHub ones. LEETCODE_AUTH_USERNAME= LEETCODE_AUTH_PASSWORD= + REDIS_URL=redis://localhost:6379/0 -GPRC_LASS_HOST=localhost:50051 -GPRC_LASS_TLS=false +GRPC_LASS_HOST=localhost:50051 +GRPC_LASS_TLS=false +# To use staging LASS (must be on VPN), replace the host and TLS values above: +# GRPC_LASS_HOST=stg.codebloom-leetcode-auth-stealer-service.vpn.patinanetwork.org:50051 +# GRPC_LASS_TLS=true GRPC_LASS_TIMEOUT_SECONDS=180 REFRESH_LOCK_TTL_SECONDS=210 REFRESH_CHECK_INTERVAL_SECONDS=900 diff --git a/infra/Dockerfile b/infra/Dockerfile index d8c14058f..c2b536d86 100644 --- a/infra/Dockerfile +++ b/infra/Dockerfile @@ -51,9 +51,6 @@ FROM mirror.gcr.io/library/eclipse-temurin:25-jre-jammy WORKDIR /app -COPY mvnw pom.xml ./ -COPY .mvn/ .mvn/ -RUN ./mvnw -B -ntp exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install --with-deps" COPY --from=backend-build /app/target/*.jar app.jar ARG SERVER_PROFILES=prod diff --git a/internal/leetcode-auth-service/Cargo.lock b/internal/leetcode-auth-service/Cargo.lock index d99a49aac..7e5f5ba26 100644 --- a/internal/leetcode-auth-service/Cargo.lock +++ b/internal/leetcode-auth-service/Cargo.lock @@ -1594,7 +1594,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" dependencies = [ "aws-lc-rs", + "log", "once_cell", + "ring", "rustls-pki-types", "rustls-webpki", "subtle", @@ -1997,12 +1999,14 @@ dependencies = [ "socket2", "sync_wrapper", "tokio", + "tokio-rustls", "tokio-stream", "tokio-util", "tower", "tower-layer", "tower-service", "tracing", + "webpki-roots", ] [[package]] @@ -2343,6 +2347,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki-roots" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5f280bbb2b287bc8e7de7e3852945a0ff50d1bbcb5ed42b6c7a568c546df171" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/internal/leetcode-auth-service/Cargo.toml b/internal/leetcode-auth-service/Cargo.toml index 80b660055..4193fb6dd 100644 --- a/internal/leetcode-auth-service/Cargo.toml +++ b/internal/leetcode-auth-service/Cargo.toml @@ -5,7 +5,8 @@ edition = "2024" rust-version = "1.94" [dependencies] -tonic = "0.14.6" +# ring + bundled webpki roots: the alpine runtime image has no system CA store +tonic = { version = "0.14.6", features = ["tls-ring", "tls-webpki-roots"] } tonic-health = "0.14.6" tonic-reflection = "0.14.6" tonic-middleware = "0.4.1" diff --git a/internal/leetcode-auth-service/README.md b/internal/leetcode-auth-service/README.md index 99a538a1a..8f2fbc4f9 100644 --- a/internal/leetcode-auth-service/README.md +++ b/internal/leetcode-auth-service/README.md @@ -44,9 +44,10 @@ docker build -f internal/leetcode-auth-service/Dockerfile internal - `REDIS_URL`: required. - `LEETCODE_AUTH_USERNAME`, `LEETCODE_AUTH_PASSWORD`: required, sent to LASS. -- `LASS_URL`: LASS gRPC address, default `http://localhost:50051`. -- `LASS_TIMEOUT_SECONDS`: deadline for one LASS login, default `180`. -- `REFRESH_LOCK_TTL_SECONDS`: how long the refresh lock lives if it is never released, default `LASS_TIMEOUT_SECONDS + 30`. Keep it above `LASS_TIMEOUT_SECONDS`, or a slow refresh can lose its lock and a second pod may refresh at the same time. +- `GRPC_LASS_HOST`: LASS gRPC host and port, without a URI scheme; default `localhost:50051`. +- `GRPC_LASS_TLS`: `true` selects HTTPS; `false` selects HTTP (default `false`). +- `GRPC_LASS_TIMEOUT_SECONDS`: deadline for one LASS login, default `180`. +- `REFRESH_LOCK_TTL_SECONDS`: how long the refresh lock lives if it is never released, default `GRPC_LASS_TIMEOUT_SECONDS + 30`. Keep it above `GRPC_LASS_TIMEOUT_SECONDS`, or a slow refresh can lose its lock and a second pod may refresh at the same time. - `REFRESH_CHECK_INTERVAL_SECONDS`: how often the stored entry's age is checked, default `900` (15 minutes). - `TOKEN_MAX_AGE_SECONDS`: age at which the scheduled check refreshes, default `14400` (4 hours). - `GRPC_PORT` / `HTTP_PORT`: default `50052` / `8082`. diff --git a/internal/leetcode-auth-service/src/config.rs b/internal/leetcode-auth-service/src/config.rs index 9797a1710..49cebea86 100644 --- a/internal/leetcode-auth-service/src/config.rs +++ b/internal/leetcode-auth-service/src/config.rs @@ -70,7 +70,7 @@ impl Config { .flatten() .unwrap_or_else(|| false); - format!("{}{host}", if tls { "http://" } else { "https://" }) + format!("{}{host}", if tls { "https://" } else { "http://" }) }, grpc_lass_timeout: lass_timeout, refresh_lock_ttl: seconds( diff --git a/internal/leetcode-auth-service/src/grpc/client/lass.rs b/internal/leetcode-auth-service/src/grpc/client/lass.rs index 3de2db0bb..57b15bd7d 100644 --- a/internal/leetcode-auth-service/src/grpc/client/lass.rs +++ b/internal/leetcode-auth-service/src/grpc/client/lass.rs @@ -3,7 +3,7 @@ use std::time::Duration; use anyhow::Context; use tonic::{ Status, - transport::{Channel, Endpoint}, + transport::{Channel, ClientTlsConfig, Endpoint}, }; use crate::{ @@ -31,11 +31,18 @@ impl GrpcLASS { /// Connects lazily so this service starts, and keeps serving stored credentials, while LASS is /// down. pub fn new(config: &Config) -> anyhow::Result { - let channel = Endpoint::from_shared(config.grpc_lass_url.clone()) - .context("LASS_URL must be a valid URI")? + let mut endpoint = Endpoint::from_shared(config.grpc_lass_url.clone()) + .context("GRPC_LASS_HOST must form a valid URI with the GRPC_LASS_TLS scheme")? .connect_timeout(Duration::from_secs(5)) - .timeout(config.grpc_lass_timeout) - .connect_lazy(); + .timeout(config.grpc_lass_timeout); + + if config.grpc_lass_url.starts_with("https://") { + endpoint = endpoint + .tls_config(ClientTlsConfig::new().with_webpki_roots()) + .context("failed to configure TLS for LASS")?; + } + + let channel = endpoint.connect_lazy(); Ok(Self { client: LeetcodeAuthStealerServiceClient::new(channel), diff --git a/pom.xml b/pom.xml index 82fac982c..e9333c150 100644 --- a/pom.xml +++ b/pom.xml @@ -137,11 +137,6 @@ springdoc-openapi-starter-webmvc-ui 2.8.2 - - com.microsoft.playwright - playwright - 1.63.0 - net.dv8tion JDA @@ -173,11 +168,6 @@ org.springframework.boot spring-boot-starter-aop - - redis.clients - jedis - 7.0.0 - org.springframework.boot spring-boot-starter-actuator diff --git a/src/README.md b/src/README.md index a035fccf7..c65087a00 100644 --- a/src/README.md +++ b/src/README.md @@ -47,8 +47,7 @@ src/main │   │   ├── dto # dtos │   │   ├── email │   │   │   ├── client -│   │   │   │   ├── codebloom # used to send emails -│   │   │   │   └── github # used to read emails during Leetcode OAuth process +│   │   │   │   └── codebloom # used to send emails │   │   │   ├── Email.java # base interface used by ./client │   │   │   ├── error # shared error used by ./client │   │   │   ├── Message.java # base message object used by ./client @@ -56,6 +55,7 @@ src/main │   │   │   └── template # react email templater │   │   ├── jwt # jwt helper class(es) │   │   ├── leetcode # leetcode client classes +│   │   │   ├── auth # credentials supplied by leetcode-auth-service over gRPC │   │   │   ├── LeetcodeClient.java │   │   │   ├── LeetcodeClientImpl.java │   │   │   ├── models # shared objects belonging to leetcode.com @@ -64,7 +64,6 @@ src/main │   │   │   └── throttled # throttled to help alleviate rate limits │   │   │   ├── ThrottledLeetcodeClient.java │   │   │   └── ThrottledLeetcodeClientImpl.java -│   │   ├── redis # WIP: redis │   │   ├── reporter # classes that can be used to report logs & errors to our private Discord server │   │   ├── schools # school-specific utils │   │   ├── security # security/auth helper utils @@ -84,7 +83,6 @@ src/main │   ├── playwright # external playwright client │   │   └── PlaywrightClient.java │   ├── scheduled # holds all background processes -│   │   ├── auth # used to "steal" a leetcode token. this is a special class as runs in the bg and can be imported into other classes (this separation should be more distinct in the future) │   │   ├── discord # used to send weekly leaderboard to discord │   │   ├── duel # WIP: used to cleanup expired duels │   │   ├── leetcode # various bg services related to leetcode.com and Codebloom @@ -207,7 +205,7 @@ public class ApiController { ### `LeetcodeClient` -We have a Leetcode client that interfaces with leetcode.com's GraphQL API to fetch question data, user submissions, and other information. It handles authentication by automatically stealing session cookies via Playwright. +We have a Leetcode client that interfaces with leetcode.com's GraphQL API to fetch question data, user submissions, and other information. It gets session credentials from the external `leetcode-auth-service` over gRPC. The main client can be found at [`LeetcodeClientImpl.java`](https://github.com/tahminator/codebloom/blob/main/src/main/java/org/patinanetwork/codebloom/common/leetcode/LeetcodeClientImpl.java), though we only use [`ThrottledLeetcodeClient.java`](https://github.com/tahminator/codebloom/blob/main/src/main/java/org/patinanetwork/codebloom/common/leetcode/throttled/ThrottledLeetcodeClientImpl.java) to avoid rate-limiting. @@ -227,12 +225,12 @@ The [`LeetcodeClient.java`](https://github.com/tahminator/codebloom/blob/main/sr #### Authentication -We have a scheduled task at [`LeetcodeAuthStealer.java`](https://github.com/tahminator/codebloom/blob/main/src/main/java/org/patinanetwork/codebloom/scheduled/auth/LeetcodeAuthStealer.java) that: +[`DefaultLeetcodeAuthenticationService`](main/java/org/patinanetwork/codebloom/common/leetcode/auth/DefaultLeetcodeAuthenticationService.java) obtains the `LEETCODE_SESSION` and `csrftoken` values through the auth service's `getToken` RPC and requests background refreshes through `refreshToken`. -- Runs every 30 minutes to refresh session cookies -- Uses Playwright to automate GitHub OAuth login flow -- Stores the `LEETCODE_SESSION` cookie in our database -- Falls back to email alerts if authentication fails +- [`leetcode-auth-service`](../internal/leetcode-auth-service/README.md) owns credential storage in Redis and schedules refreshes. +- That service calls [`leetcode-auth-stealer-service`](../internal/leetcode-auth-stealer-service/README.md) to authenticate with LeetCode using email and password. +- The backend no longer runs a scheduled Playwright/GitHub OAuth stealer or stores LeetCode credentials in Postgres. The legacy `Auth` table is retained but unused by the backend; Codebloom user sessions are separate and unchanged. +- Under the `ci` Spring profile, `CiLeetcodeAuthenticationService` returns empty credentials without calling the auth service. This is necessary because leetcode.com requires authenticated requests for most API calls. @@ -295,21 +293,18 @@ Codebloom has a hand-rolled error reporter that will report certain logs and err #### Examples -[Link to Reporter example](https://github.com/tahminator/codebloom/blob/main/src/main/java/org/patinanetwork/codebloom/scheduled/auth/LeetcodeAuthStealer.java#L244-L259) +[Link to Reporter example](main/java/org/patinanetwork/codebloom/common/utils/log/LogExecutionTimeAspect.java) ```java - public synchronized String getCsrf() { - if (csrf == null && !reported) { - reported = true; - reporter.log(Report.builder() - .environments(env.getActiveProfiles()) - .location(Location.BACKEND) - .data("CSRF token is missing inside of LeetcodeAuthStealer. This may be something to look into.") - .build()); - } - - return csrf; - } +if (logExecutionTime.reportToDiscord()) { + reporter.log( + "logExecTime", + Report.builder() + .data("%s executed in %s ms".formatted(joinPoint.getSignature(), elapsed)) + .environments(env.getActiveProfiles()) + .location(Location.BACKEND) + .build()); +} ``` [Link to ThrottledReporter example](https://github.com/tahminator/codebloom/blob/main/src/main/java/org/patinanetwork/codebloom/common/submissions/SubmissionsHandler.java#L131-L148) diff --git a/src/main/java/org/patinanetwork/codebloom/common/email/client/github/GithubOAuthEmailClient.java b/src/main/java/org/patinanetwork/codebloom/common/email/client/github/GithubOAuthEmailClient.java deleted file mode 100644 index 5a84314c7..000000000 --- a/src/main/java/org/patinanetwork/codebloom/common/email/client/github/GithubOAuthEmailClient.java +++ /dev/null @@ -1,109 +0,0 @@ -package org.patinanetwork.codebloom.common.email.client.github; - -import io.micrometer.core.annotation.Timed; -import jakarta.mail.Folder; -import jakarta.mail.Session; -import jakarta.mail.Store; -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import org.patinanetwork.codebloom.common.email.EmailClient; -import org.patinanetwork.codebloom.common.email.Message; -import org.patinanetwork.codebloom.common.email.error.EmailException; -import org.patinanetwork.codebloom.common.email.options.SendEmailOptions; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.stereotype.Component; - -/** Provides read-only access to the Github email account in order to access the OAuth code. */ -@Component -@EnableConfigurationProperties(GithubOAuthEmailClientProperties.class) -@Timed(value = "email.client.execution") -public class GithubOAuthEmailClient extends EmailClient { - - private final GithubOAuthEmailClientProperties emailClientProperties; - private static Session session; - - public GithubOAuthEmailClient(final GithubOAuthEmailClientProperties emailClientProperties) { - this.emailClientProperties = emailClientProperties; - final Properties properties = new Properties(); - properties.setProperty("mail.imap.host", emailClientProperties.getHost()); - properties.setProperty("mail.imap.port", emailClientProperties.getPort()); - properties.setProperty("mail.imap.ssl.enable", "true"); - properties.setProperty("mail.imap.auth", "true"); - properties.setProperty("mail.store.protocol", "imap"); - - session = Session.getDefaultInstance(properties); - } - - public List getPastMessages() throws EmailException { - try { - final Store store = session.getStore("imap"); - - store.connect( - emailClientProperties.getHost(), - emailClientProperties.getUsername(), - emailClientProperties.getPassword()); - - final Folder emailFolder = store.getFolder("Inbox"); - emailFolder.open(Folder.READ_ONLY); - - List messages = new ArrayList<>(); - int count = emailFolder.getMessageCount(); - for (var m : emailFolder.getMessages(count - 5, count)) { - try { - String content = null; - if (m.isMimeType("text/plain")) { - content = ""; - InputStream is = m.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - StringBuilder sb = new StringBuilder(); - String thisLine; - while ((thisLine = reader.readLine()) != null) { - sb.append(thisLine).append("\n"); - } - content = sb.toString(); - } - messages.add(new Message(m.getSubject(), content, m.getReceivedDate())); - } catch (Exception e) { - System.err.println(e); - } - } - emailFolder.close(); - store.close(); - - return messages; - } catch (Exception e) { - throw new EmailException("Something went wrong when receiving past messages", e); - } - } - - /** @deprecated - This is not supported. */ - @Override - @Deprecated - public void sendMessage(final SendEmailOptions sendEmailOptions) throws EmailException { - throw new UnsupportedOperationException("GithubOAuthEmailClient does not support sending messages."); - } - - @Override - public void testConnection() throws EmailException { - try { - final Store store = session.getStore("imap"); - - store.connect( - emailClientProperties.getHost(), - emailClientProperties.getUsername(), - emailClientProperties.getPassword()); - - final Folder emailFolder = store.getFolder("Inbox"); - emailFolder.open(Folder.READ_ONLY); - - emailFolder.close(); - store.close(); - } catch (Exception e) { - throw new EmailException("Something went wrong when testing connection", e); - } - } -} diff --git a/src/main/java/org/patinanetwork/codebloom/common/email/client/github/GithubOAuthEmailClientProperties.java b/src/main/java/org/patinanetwork/codebloom/common/email/client/github/GithubOAuthEmailClientProperties.java deleted file mode 100644 index efc813b1d..000000000 --- a/src/main/java/org/patinanetwork/codebloom/common/email/client/github/GithubOAuthEmailClientProperties.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.patinanetwork.codebloom.common.email.client.github; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties(prefix = "leetcode.auth.github.email") -public class GithubOAuthEmailClientProperties { - - private String host; - private String port; - private String type; - private String username; - private String password; - - public String getHost() { - return host; - } - - public void setHost(final String host) { - this.host = host; - } - - public String getPort() { - return port; - } - - public void setPort(final String port) { - this.port = port; - } - - public String getType() { - return type; - } - - public void setType(final String type) { - this.type = type; - } - - public String getUsername() { - return username; - } - - public void setUsername(final String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(final String password) { - this.password = password; - } -} diff --git a/src/main/java/org/patinanetwork/codebloom/common/redis/JedisClientConfiguration.java b/src/main/java/org/patinanetwork/codebloom/common/redis/JedisClientConfiguration.java deleted file mode 100644 index f959b4108..000000000 --- a/src/main/java/org/patinanetwork/codebloom/common/redis/JedisClientConfiguration.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.patinanetwork.codebloom.common.redis; - -import lombok.Getter; -import lombok.Setter; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Profile; - -@Profile("ci") -@ConfigurationProperties(prefix = "jedis") -@Getter -@Setter -public class JedisClientConfiguration { - - private String url; -} diff --git a/src/main/java/org/patinanetwork/codebloom/common/redis/JedisClientManager.java b/src/main/java/org/patinanetwork/codebloom/common/redis/JedisClientManager.java deleted file mode 100644 index 9be5611af..000000000 --- a/src/main/java/org/patinanetwork/codebloom/common/redis/JedisClientManager.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.patinanetwork.codebloom.common.redis; - -import lombok.Getter; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.stereotype.Component; -import redis.clients.jedis.UnifiedJedis; - -@Component -@EnableConfigurationProperties(JedisClientConfiguration.class) -public class JedisClientManager { - @Getter - private UnifiedJedis client; - - public JedisClientManager(JedisClientConfiguration jedisClientConfiguration) { - client = new UnifiedJedis(jedisClientConfiguration.getUrl()); - } -} diff --git a/src/main/java/org/patinanetwork/codebloom/common/redis/RedisClient.java b/src/main/java/org/patinanetwork/codebloom/common/redis/RedisClient.java deleted file mode 100644 index eb30b2793..000000000 --- a/src/main/java/org/patinanetwork/codebloom/common/redis/RedisClient.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.patinanetwork.codebloom.common.redis; - -import java.time.temporal.ChronoUnit; -import java.util.Optional; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import redis.clients.jedis.UnifiedJedis; -import redis.clients.jedis.params.SetParams; - -@Component -@Slf4j -public class RedisClient { - - private final UnifiedJedis client; - - public RedisClient(JedisClientManager clientManager) { - client = clientManager.getClient(); - } - - private void setAuth(final String auth, final long expires) { - client.set("auth", auth, SetParams.setParams().ex(expires)); - } - - /** Get auth token. */ - public Optional getAuth() { - return Optional.ofNullable(client.get("auth")); - } - - /** Set auth token and when it should be ejected from the cache. */ - public void setAuth(final String auth, final long units, final ChronoUnit chronoUnit) { - setAuth(auth, chronoUnit.getDuration().multipliedBy(units).toSeconds()); - } -} diff --git a/src/main/java/org/patinanetwork/codebloom/playwright/PlaywrightClient.java b/src/main/java/org/patinanetwork/codebloom/playwright/PlaywrightClient.java deleted file mode 100644 index efa680af4..000000000 --- a/src/main/java/org/patinanetwork/codebloom/playwright/PlaywrightClient.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.patinanetwork.codebloom.playwright; - -import com.microsoft.playwright.Page; -import com.microsoft.playwright.options.ScreenshotType; -import java.util.function.Function; -import lombok.extern.slf4j.Slf4j; -import org.patinanetwork.codebloom.common.db.models.usertag.Tag; -import org.patinanetwork.codebloom.common.email.client.github.GithubOAuthEmailClient; -import org.patinanetwork.codebloom.common.url.ServerUrlUtils; -import org.springframework.stereotype.Component; - -@Component -@Slf4j -public class PlaywrightClient { - - private static final String USER_AGENT = - "Mozilla/5.0 (Linux; U; Android 4.4.1; SAMSUNG SM-J210G Build/KTU84P) AppleWebKit/536.31 (KHTML, like Gecko) Chrome/48.0.2090.359 Mobile Safari/601.9"; - - private final ServerUrlUtils serverUrlUtils; - private final PlaywrightProvider playwrightProvider; - - public PlaywrightClient( - final ServerUrlUtils serverUrlUtils, - final GithubOAuthEmailClient githubOAuthEmailClient, - final PlaywrightProvider playwrightProvider) { - this.serverUrlUtils = serverUrlUtils; - this.playwrightProvider = playwrightProvider; - } - - private T withPage(final Function consumer) { - return playwrightProvider.withPage(USER_AGENT, consumer); - } - - public byte[] getCodebloomLeaderboardScreenshot(final int page, final Tag tag) { - log.info("Loading page {} for screenshot...", page); - return withPage(p -> { - p.navigate("%s/leaderboard?%s=true&page=%s" - .formatted(serverUrlUtils.getUrl(), tag.name().toLowerCase(), page)); - p.waitForTimeout(5_000); - byte[] screenshot = p.screenshot( - new Page.ScreenshotOptions().setType(ScreenshotType.PNG).setFullPage(true)); - return screenshot; - }); - } -} diff --git a/src/main/java/org/patinanetwork/codebloom/playwright/PlaywrightProvider.java b/src/main/java/org/patinanetwork/codebloom/playwright/PlaywrightProvider.java deleted file mode 100644 index 78e22292a..000000000 --- a/src/main/java/org/patinanetwork/codebloom/playwright/PlaywrightProvider.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.patinanetwork.codebloom.playwright; - -import com.microsoft.playwright.Browser; -import com.microsoft.playwright.Browser.NewContextOptions; -import com.microsoft.playwright.BrowserContext; -import com.microsoft.playwright.BrowserType; -import com.microsoft.playwright.Page; -import com.microsoft.playwright.Playwright; -import jakarta.annotation.PostConstruct; -import java.util.function.Function; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; - -@Service -@Slf4j -public class PlaywrightProvider { - - private final boolean headless; - - public PlaywrightProvider(@Value("${playwright.headless}") final boolean headless) { - this.headless = headless; - } - - @PostConstruct - void init() { - log.info("Playwright headless={}", headless); - } - - public T withPage(final String userAgent, final Function consumer) { - NewContextOptions options = new NewContextOptions(); - if (userAgent != null) { - options.setUserAgent(userAgent); - } - - try (Playwright playwright = Playwright.create(); - Browser browser = playwright - .firefox() - .launch(new BrowserType.LaunchOptions() - .setHeadless(headless) - .setTimeout(40000)); - BrowserContext context = browser.newContext(options); - Page page = context.newPage()) { - return consumer.apply(page); - } - } - - public T withPage(final Function consumer) { - return withPage(null, consumer); - } -} diff --git a/src/main/java/org/patinanetwork/codebloom/utilities/generator/complex/ComplexJSTypesGenerator.java b/src/main/java/org/patinanetwork/codebloom/utilities/generator/complex/ComplexJSTypesGenerator.java index bde3d687c..9796287c2 100644 --- a/src/main/java/org/patinanetwork/codebloom/utilities/generator/complex/ComplexJSTypesGenerator.java +++ b/src/main/java/org/patinanetwork/codebloom/utilities/generator/complex/ComplexJSTypesGenerator.java @@ -215,14 +215,21 @@ private void writeToFile() throws Exception { Path nodeExecutable = resolveNodeExecutable(); Path prettierBin = Paths.get("js/node_modules/prettier/bin/prettier.cjs").toAbsolutePath(); - new ProcessBuilder( + + // Don't inherit IO: under surefire, stdin/stdout are the test runner's command channel, and a + // child writing to or reading from them corrupts it. Capture output and log it instead. + Process prettier = new ProcessBuilder( nodeExecutable.toString(), prettierBin.toString(), "--write", outputPath.toAbsolutePath().toString()) - .inheritIO() - .start() - .waitFor(); + .redirectErrorStream(true) + .start(); + prettier.getOutputStream().close(); + try (var output = prettier.inputReader()) { + output.lines().forEach(line -> log.info("[prettier] {}", line)); + } + prettier.waitFor(); log.info("Generated TypeScript file: {} with {} import(s)", outputPath, imports.size()); } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 5dd1aa054..ffc8294d9 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -72,22 +72,6 @@ grpc: # both RPCs answer from Redis in milliseconds; refreshToken returns before the login runs timeout: ${GRPC_LAS_TIMEOUT:1s} -leetcode: - auth: - strategy: direct # can be "direct" or "github" - direct: - username: ${LEETCODE_AUTH_USERNAME} - password: ${LEETCODE_AUTH_PASSWORD} - github: - username: ${GH_USERNAME} - password: ${GH_PASSWORD} - email: - host: ${GH_EMAIL_HOST} - port: ${GH_EMAIL_PORT} - type: ${GH_EMAIL_TYPE} - username: ${GH_EMAIL_USERNAME} - password: ${GH_EMAIL_PASSWORD} - jda: discord: token: ${DISCORD_TOKEN} @@ -133,13 +117,6 @@ app: commit: sha: "${VERSION:@commit.sha@}" -jedis: - url: ${REDIS_URL} - -playwright: - # change to false in application-dev.yaml to see Playwright browser attempting auth - headless: true - ff: # override per environment as needed duels: false diff --git a/src/test/java/org/patinanetwork/codebloom/common/components/DiscordClubManagerTest.java b/src/test/java/org/patinanetwork/codebloom/common/components/DiscordClubManagerTest.java index d3599e8b1..7ffc67f91 100644 --- a/src/test/java/org/patinanetwork/codebloom/common/components/DiscordClubManagerTest.java +++ b/src/test/java/org/patinanetwork/codebloom/common/components/DiscordClubManagerTest.java @@ -34,7 +34,6 @@ import org.patinanetwork.codebloom.common.url.ServerUrlUtils; import org.patinanetwork.codebloom.jda.client.JDAClient; import org.patinanetwork.codebloom.jda.client.options.EmbeddedImagesMessageOptions; -import org.patinanetwork.codebloom.playwright.PlaywrightClient; import org.slf4j.LoggerFactory; public class DiscordClubManagerTest { @@ -42,7 +41,6 @@ public class DiscordClubManagerTest { private JDAClient jdaClient = mock(JDAClient.class); private LeaderboardRepository leaderboardRepository = mock(LeaderboardRepository.class); private DiscordClubRepository discordClubRepository = mock(DiscordClubRepository.class); - private PlaywrightClient playwrightClient = mock(PlaywrightClient.class); private ServerUrlUtils serverUrlUtils = mock(ServerUrlUtils.class); private UserRepository userRepository = mock(UserRepository.class); private LeaderboardManager leaderboardManager = mock(LeaderboardManager.class); @@ -634,9 +632,6 @@ private void setupMockLeaderboardData() { when(leaderboardRepository.getLeaderboardUsersById(eq("leaderboard-id"), any(LeaderboardFilterOptions.class))) .thenReturn(mockUsers); - when(playwrightClient.getCodebloomLeaderboardScreenshot(anyInt(), any(Tag.class))) - .thenReturn("mock-screenshot".getBytes()); - when(serverUrlUtils.getUrl()).thenReturn("http://localhost:3000"); } diff --git a/src/test/java/org/patinanetwork/codebloom/common/db/repos/BaseRepositoryTest.java b/src/test/java/org/patinanetwork/codebloom/common/db/repos/BaseRepositoryTest.java index 1bd33fe24..c53336d5b 100644 --- a/src/test/java/org/patinanetwork/codebloom/common/db/repos/BaseRepositoryTest.java +++ b/src/test/java/org/patinanetwork/codebloom/common/db/repos/BaseRepositoryTest.java @@ -1,7 +1,6 @@ package org.patinanetwork.codebloom.common.db.repos; import org.patinanetwork.codebloom.common.email.client.codebloom.OfficialCodebloomEmailClient; -import org.patinanetwork.codebloom.common.email.client.github.GithubOAuthEmailClient; import org.patinanetwork.codebloom.common.leetcode.auth.LeetcodeAuthenticationService; import org.patinanetwork.codebloom.jda.JDAClientManager; import org.patinanetwork.codebloom.jda.command.JDASlashCommandInitializer; @@ -24,9 +23,6 @@ public class BaseRepositoryTest { @MockitoBean private OfficialCodebloomEmailClient codebloomEmailClient; - @MockitoBean - private GithubOAuthEmailClient githubOAuthEmailClient; - @MockitoBean private SubmissionScheduler submissionScheduler; diff --git a/src/test/java/org/patinanetwork/codebloom/common/email/TestEmailClients.java b/src/test/java/org/patinanetwork/codebloom/common/email/TestEmailClients.java index 7631dad1f..ceab96140 100644 --- a/src/test/java/org/patinanetwork/codebloom/common/email/TestEmailClients.java +++ b/src/test/java/org/patinanetwork/codebloom/common/email/TestEmailClients.java @@ -2,7 +2,6 @@ import org.junit.jupiter.api.Test; import org.patinanetwork.codebloom.common.email.client.codebloom.OfficialCodebloomEmailClient; -import org.patinanetwork.codebloom.common.email.client.github.GithubOAuthEmailClient; import org.patinanetwork.codebloom.common.email.error.EmailException; import org.patinanetwork.codebloom.config.NoJdaRequired; import org.springframework.beans.factory.annotation.Autowired; @@ -11,20 +10,15 @@ @SpringBootTest public class TestEmailClients extends NoJdaRequired { - private final GithubOAuthEmailClient githubOAuthEmailClient; private final OfficialCodebloomEmailClient officialCodebloomEmailClient; @Autowired - public TestEmailClients( - final GithubOAuthEmailClient githubOAuthEmailClient, - final OfficialCodebloomEmailClient officialCodebloomEmailClient) { - this.githubOAuthEmailClient = githubOAuthEmailClient; + public TestEmailClients(final OfficialCodebloomEmailClient officialCodebloomEmailClient) { this.officialCodebloomEmailClient = officialCodebloomEmailClient; } @Test void testConnections() throws EmailException { - githubOAuthEmailClient.testConnection(); officialCodebloomEmailClient.testConnection(); } } diff --git a/src/test/java/org/patinanetwork/codebloom/common/email/client/github/GithubOAuthEmailClientTest.java b/src/test/java/org/patinanetwork/codebloom/common/email/client/github/GithubOAuthEmailClientTest.java deleted file mode 100644 index 356b01d76..000000000 --- a/src/test/java/org/patinanetwork/codebloom/common/email/client/github/GithubOAuthEmailClientTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.patinanetwork.codebloom.common.email.client.github; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import jakarta.mail.Folder; -import jakarta.mail.Session; -import jakarta.mail.Store; -import java.io.ByteArrayInputStream; -import java.util.Date; -import org.junit.jupiter.api.Test; -import org.patinanetwork.codebloom.common.email.error.EmailException; -import org.patinanetwork.codebloom.common.email.options.SendEmailOptions; - -class GithubOAuthEmailClientTest { - - @Test - void testSendMessageThrowsUnsupportedOperationException() { - GithubOAuthEmailClientProperties properties = new GithubOAuthEmailClientProperties(); - properties.setHost("imap.gmail.com"); - properties.setPort("993"); - - GithubOAuthEmailClient client = new GithubOAuthEmailClient(properties); - - SendEmailOptions options = SendEmailOptions.builder() - .recipientEmail("recipient@example.com") - .subject("Test") - .body("Test content") - .build(); - - assertThrows(UnsupportedOperationException.class, () -> client.sendMessage(options)); - } - - @Test - void testGetPastMessagesThrowsEmailException() { - GithubOAuthEmailClientProperties properties = new GithubOAuthEmailClientProperties(); - properties.setHost("invalid.host.example"); - properties.setPort("993"); - - GithubOAuthEmailClient client = new GithubOAuthEmailClient(properties); - - EmailException exception = assertThrows(EmailException.class, client::getPastMessages); - assertEquals("Something went wrong when receiving past messages", exception.getMessage()); - } - - @Test - void testConnectionThrowsEmailException() { - GithubOAuthEmailClientProperties properties = new GithubOAuthEmailClientProperties(); - properties.setHost("invalid.host.example"); - properties.setPort("993"); - - GithubOAuthEmailClient client = new GithubOAuthEmailClient(properties); - - EmailException exception = assertThrows(EmailException.class, client::testConnection); - assertEquals("Something went wrong when testing connection", exception.getMessage()); - } - - @Test - void testGetPastMessagesReadsTextPlainContent() throws Exception { - GithubOAuthEmailClientProperties properties = new GithubOAuthEmailClientProperties(); - properties.setHost("imap.gmail.com"); - properties.setPort("993"); - - Store mockStore = mock(Store.class); - Folder mockFolder = mock(Folder.class); - jakarta.mail.Message mockMessage = mock(jakarta.mail.Message.class); - Session mockSession = mock(Session.class); - - when(mockMessage.isMimeType("text/plain")).thenReturn(true); - when(mockMessage.getInputStream()).thenReturn(new ByteArrayInputStream("test".getBytes())); - when(mockMessage.getSubject()).thenReturn("Subject"); - when(mockMessage.getReceivedDate()).thenReturn(new Date()); - when(mockFolder.getMessages(anyInt(), anyInt())).thenReturn(new jakarta.mail.Message[] {mockMessage}); - when(mockFolder.getMessageCount()).thenReturn(1); - when(mockStore.getFolder("Inbox")).thenReturn(mockFolder); - when(mockSession.getStore("imap")).thenReturn(mockStore); - - GithubOAuthEmailClient client = new GithubOAuthEmailClient(properties); - java.lang.reflect.Field sessionField = GithubOAuthEmailClient.class.getDeclaredField("session"); - sessionField.setAccessible(true); - sessionField.set(client, mockSession); - - assertEquals(1, client.getPastMessages().size()); - } - - @Test - void testGetPastMessagesCatchesException() throws Exception { - GithubOAuthEmailClientProperties properties = new GithubOAuthEmailClientProperties(); - properties.setHost("imap.gmail.com"); - properties.setPort("993"); - - Store mockStore = mock(Store.class); - Folder mockFolder = mock(Folder.class); - jakarta.mail.Message mockMessage = mock(jakarta.mail.Message.class); - Session mockSession = mock(Session.class); - - when(mockMessage.isMimeType("text/plain")).thenThrow(new RuntimeException()); - when(mockFolder.getMessages(anyInt(), anyInt())).thenReturn(new jakarta.mail.Message[] {mockMessage}); - when(mockFolder.getMessageCount()).thenReturn(1); - when(mockStore.getFolder("Inbox")).thenReturn(mockFolder); - when(mockSession.getStore("imap")).thenReturn(mockStore); - - GithubOAuthEmailClient client = new GithubOAuthEmailClient(properties); - java.lang.reflect.Field sessionField = GithubOAuthEmailClient.class.getDeclaredField("session"); - sessionField.setAccessible(true); - sessionField.set(client, mockSession); - - assertEquals(0, client.getPastMessages().size()); - } -} diff --git a/src/test/java/org/patinanetwork/codebloom/playwright/PlaywrightClientTest.java b/src/test/java/org/patinanetwork/codebloom/playwright/PlaywrightClientTest.java deleted file mode 100644 index 89820a62f..000000000 --- a/src/test/java/org/patinanetwork/codebloom/playwright/PlaywrightClientTest.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.patinanetwork.codebloom.playwright; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; - -import com.microsoft.playwright.Page; -import com.microsoft.playwright.Page.ScreenshotOptions; -import java.util.function.Function; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.patinanetwork.codebloom.common.db.models.usertag.Tag; -import org.patinanetwork.codebloom.common.email.client.github.GithubOAuthEmailClient; -import org.patinanetwork.codebloom.common.url.ServerUrlUtils; - -public class PlaywrightClientTest { - - private ServerUrlUtils serverUrlUtils = mock(ServerUrlUtils.class); - private GithubOAuthEmailClient emailClient = mock(GithubOAuthEmailClient.class); - private PlaywrightProvider playwrightProvider = mock(PlaywrightProvider.class); - - private Page page = mock(Page.class); - - private PlaywrightClient playwrightClient; - - @BeforeEach - public void setup() { - playwrightClient = new PlaywrightClient(serverUrlUtils, emailClient, playwrightProvider); - - when(playwrightProvider.withPage(anyString(), any())).thenAnswer(p -> { - Function fn = p.getArgument(1); - return fn.apply(page); - }); - - byte[] screenshot = new byte[] {1, 2, 3}; - when(page.screenshot(any(ScreenshotOptions.class))).thenReturn(screenshot); - } - - @Test - void testGetCodebloomLeaderboardScreenshotSuccess() { - when(serverUrlUtils.getUrl()).thenReturn("https://stg.codebloom.patinanetwork.org"); - - byte[] expected = new byte[] {1, 2, 3}; - when(page.screenshot(any(ScreenshotOptions.class))).thenReturn(expected); - - byte[] screenshot = playwrightClient.getCodebloomLeaderboardScreenshot(1, Tag.Rpi); - - assertNotNull(screenshot, "Screenshot should not be null"); - assertTrue(screenshot.length > 0, "Screenshot should have content"); - - verify(page).navigate("https://stg.codebloom.patinanetwork.org/leaderboard?rpi=true&page=1"); - verify(serverUrlUtils).getUrl(); - } - - @Test - void testGetCodebloomLeaderboardScreenshotDifferentPage() { - when(serverUrlUtils.getUrl()).thenReturn("https://stg.codebloom.patinanetwork.org"); - - byte[] screenshot = playwrightClient.getCodebloomLeaderboardScreenshot(2, Tag.Baruch); - - assertNotNull(screenshot, "Screenshot should not be null"); - assertTrue(screenshot.length > 0, "Screenshot should have content"); - - verify(page).navigate("https://stg.codebloom.patinanetwork.org/leaderboard?baruch=true&page=2"); - verify(serverUrlUtils).getUrl(); - } - - @Test - void testGetCodebloomLeaderboardScreenshotDifferentTag() { - when(serverUrlUtils.getUrl()).thenReturn("https://stg.codebloom.patinanetwork.org"); - - byte[] screenshot = playwrightClient.getCodebloomLeaderboardScreenshot(1, Tag.Gwc); - - assertNotNull(screenshot, "Screenshot should not be null"); - assertTrue(screenshot.length > 0, "Screenshot should have content"); - - verify(page).navigate("https://stg.codebloom.patinanetwork.org/leaderboard?gwc=true&page=1"); - verify(serverUrlUtils).getUrl(); - } -}