name: Smoke test (PR)

permissions:
  contents: read

on: 
  pull_request_target:
    types:
      - labeled

env:
  python-version: "3.12"

concurrency: 
  group: "smoke-test" # Make sure, to not run multiple smoke tests at the same time
  cancel-in-progress: false

jobs:
  smoke_test_pr:
    runs-on: ubuntu-latest
    if: ${{ github.event.label.name == 'run smoke test' }}
    strategy:
      matrix:
        country: 
          - ch
          - de
          - pl
          - ma
          - ie
    steps:
      - name: Get User Permission
        id: checkAccess
        uses: actions-cool/check-user-permission@v2
        with:
          require: read
          username: ${{ github.triggering_actor }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Check User Permission
        if: steps.checkAccess.outputs.require-result == 'false'
        run: |
          echo "${{ github.triggering_actor }} does not have permissions on this repo."
          echo "Current permission level is ${{ steps.checkAccess.outputs.user-permission }}"
          echo "Job originally triggered by ${{ github.actor }}"
          exit 1
      - name: Checkout current code
        uses: actions/checkout@v6
      - name: Set up Python ${{ env.python-version }}
        uses: actions/setup-python@v6
        with:
          python-version: ${{ env.python-version }}
      - name: Check secret access
        run: |
          if [[ "x${{ secrets.PASSWORD }}" != "x" ]]; then
            echo "Access to secrets"
          else
            echo "No access to secrets"
            exit 1
          fi
      - name: Install dependencies
        run: |
            python -m pip install --upgrade pip
            pip install -r requirements.txt
            pip install -r requirements_test.txt
      - name: Prepare empty token
        run: touch .token
      - name: Prepare cookies with pytest
        run: pytest smoke_test/test_1_setup_token.py -v -x
        env:
          COUNTRY: ${{ matrix.country }}
          EMAIL_CH: ${{ vars.EMAIL_CH }}
          EMAIL_DE: ${{ vars.EMAIL_DE }}
          EMAIL_PL: ${{ vars.EMAIL_PL }}
          EMAIL_MA: ${{ vars.EMAIL_MA }}
          EMAIL_IE: ${{ vars.EMAIL_IE }}
          PASSWORD: ${{ secrets.PASSWORD }}
      # !!!!!!!!!!!!!!!!!!!!
      # From here on, we run new code which does not have access to the password, only the token
      # !!!!!!!!!!!!!!!!!!!!
      - name: Checkout new code
        uses: actions/checkout@v6
        with:
          ref: "${{ github.event.pull_request.merge_commit_sha }}"
          clean: false
      - name: Test with pytest
        run: pytest smoke_test/test_2_methods.py -v -x
        env:
          COUNTRY: ${{ matrix.country }}
          EMAIL_CH: ${{ vars.EMAIL_CH }}
          EMAIL_DE: ${{ vars.EMAIL_DE }}
          EMAIL_PL: ${{ vars.EMAIL_PL }}
          EMAIL_MA: ${{ vars.EMAIL_MA }}
          EMAIL_IE: ${{ vars.EMAIL_IE }}
          PASSWORD: <redacted>
          