name: Cypress

# Controls when the action will run.
on: [pull_request, workflow_dispatch]

env:
  LANG: "de_DE.UTF-8"

jobs:
  # Label of the container job
  cypress-mobile:
    strategy:
      fail-fast: false
      matrix:
        config: [1, 2, 3]

    # Containers must run in Linux based operating systems
    runs-on: ubuntu-22.04

    # Service containers to run with `container-job`
    services:
      # Label used to access the service container
      postgres:
        # Docker Hub image
        image: mdillon/postgis
        # Provide the password for postgres
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: eventcally_tests
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          # Maps tcp port 5432 on service container to the host
          - 5432:5432

    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.10"

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Start server
        run: |
          flask test create-all
          flask test seed
          gunicorn -b 127.0.0.1:5001 -w 1 project:app --daemon
        env:
          DATABASE_URL: postgresql://postgres:postgres@localhost/eventcally_tests
          TESTING: 1
          AUTHLIB_INSECURE_TRANSPORT: 1
          JWT_PRIVATE_KEY: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAxMl+DFiwhnoWRiVPcEIstAW66VY39JNo1TG1U10OfaFMuWI8\njcq5VfSRPBdXLRqAJwZi4CZCwOglEvURArf1QTxVBdLpDW75tD6fNOHaHKvX5WPR\nMGC/7Rfo9K/OgEWnQplc/0Me9D8MzTpIA56Uo/H/DE82kGWOMcWdqIA40pHaCZ7i\nWIoD7pS6DcRDt/8o0+IQqhMC4SrjL2HelzXHpg4glgY+GLbvTPOfV/OlFCAWDK3o\nN3wMBYS+TFP2Q/5pXfPCajIrSfF7dHyZU2d/K26A8naKcSUYuJzA8GBXkPx8YObt\nOS5SYYeD5GeGsX3IsVVyk3WqMVN7NXvRqmNjqQIDAQABAo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QC/wOD9Wv6IQLR7f5GQSXrlmiCGmUugd3BG7UXrevYzbm48ePyIIa+e\nPaSvTkZR5Ji5XvajSDhYK1/2qmozk6B1pH1M4jqKD80zZOq8mc9q8lZ9fEIxi5wz\nPVHRdVKT+wEsbeQhxqX/RfaqIi1UcBmwhnZKXutXPon/ZIg7iVoo5g==\n-----END RSA PRIVATE KEY-----\n'
          JWT_PUBLIC_JWKS: '{"keys":[{"kid":"default","kty":"RSA","use":"sig","alg":"RS256","n":"xMl-DFiwhnoWRiVPcEIstAW66VY39JNo1TG1U10OfaFMuWI8jcq5VfSRPBdXLRqAJwZi4CZCwOglEvURArf1QTxVBdLpDW75tD6fNOHaHKvX5WPRMGC_7Rfo9K_OgEWnQplc_0Me9D8MzTpIA56Uo_H_DE82kGWOMcWdqIA40pHaCZ7iWIoD7pS6DcRDt_8o0-IQqhMC4SrjL2HelzXHpg4glgY-GLbvTPOfV_OlFCAWDK3oN3wMBYS-TFP2Q_5pXfPCajIrSfF7dHyZU2d_K26A8naKcSUYuJzA8GBXkPx8YObtOS5SYYeD5GeGsX3IsVVyk3WqMVN7NXvRqmNjqQ","e":"AQAB"}]}'

      - name: Cypress run
        uses: cypress-io/github-action@v5
        with:
          browser: chrome
          record: true
          parallel: true
          group: "Mobile"
          wait-on: "http://127.0.0.1:5001"
          config-file: cypress/config/mobile.config.js
          config: baseUrl=http://127.0.0.1:5001
        env:
          DATABASE_URL: postgresql://postgres:postgres@localhost/eventcally_tests
          TESTING: 1
          CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }}
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  # TODO: Optimize because this is just a copy of the first job
  cypress-desktop:
    strategy:
      fail-fast: false
      matrix:
        config: [1, 2, 3]

    # Containers must run in Linux based operating systems
    runs-on: ubuntu-22.04

    # Service containers to run with `container-job`
    services:
      # Label used to access the service container
      postgres:
        # Docker Hub image
        image: mdillon/postgis
        # Provide the password for postgres
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: eventcally_tests
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          # Maps tcp port 5432 on service container to the host
          - 5432:5432

    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.10"

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Start server
        run: |
          flask test create-all
          flask test seed
          gunicorn -b 127.0.0.1:5001 -w 1 project:app --daemon
        env:
          DATABASE_URL: postgresql://postgres:postgres@localhost/eventcally_tests
          TESTING: 1
          AUTHLIB_INSECURE_TRANSPORT: 1
          JWT_PRIVATE_KEY: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAxMl+DFiwhnoWRiVPcEIstAW66VY39JNo1TG1U10OfaFMuWI8\njcq5VfSRPBdXLRqAJwZi4CZCwOglEvURArf1QTxVBdLpDW75tD6fNOHaHKvX5WPR\nMGC/7Rfo9K/OgEWnQplc/0Me9D8MzTpIA56Uo/H/DE82kGWOMcWdqIA40pHaCZ7i\nWIoD7pS6DcRDt/8o0+IQqhMC4SrjL2HelzXHpg4glgY+GLbvTPOfV/OlFCAWDK3o\nN3wMBYS+TFP2Q/5pXfPCajIrSfF7dHyZU2d/K26A8naKcSUYuJzA8GBXkPx8YObt\nOS5SYYeD5GeGsX3IsVVyk3WqMVN7NXvRqmNjqQIDAQABAoIBABYoJbQYLFAH4Qgm\n3UnjeYsPdhsdpRE5o70VargnKyGEDtJ0uCKy56lA7zVX323ppPI/6tQYGr4DpWD8\nDN1Efy3IMD6Gn8zuaNkdj+lxYJCH3LgsQ/pxekqkvYsHXHXe4dVC1VC8avjPPI7C\n+CbILS5EahYluRwLAzahbH3zafJzXK6qxVCaPBn1QA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IVATE KEY-----\n'
          JWT_PUBLIC_JWKS: '{"keys":[{"kid":"default","kty":"RSA","use":"sig","alg":"RS256","n":"xMl-DFiwhnoWRiVPcEIstAW66VY39JNo1TG1U10OfaFMuWI8jcq5VfSRPBdXLRqAJwZi4CZCwOglEvURArf1QTxVBdLpDW75tD6fNOHaHKvX5WPRMGC_7Rfo9K_OgEWnQplc_0Me9D8MzTpIA56Uo_H_DE82kGWOMcWdqIA40pHaCZ7iWIoD7pS6DcRDt_8o0-IQqhMC4SrjL2HelzXHpg4glgY-GLbvTPOfV_OlFCAWDK3oN3wMBYS-TFP2Q_5pXfPCajIrSfF7dHyZU2d_K26A8naKcSUYuJzA8GBXkPx8YObtOS5SYYeD5GeGsX3IsVVyk3WqMVN7NXvRqmNjqQ","e":"AQAB"}]}'

      - name: Cypress run
        uses: cypress-io/github-action@v5
        with:
          browser: chrome
          record: true
          parallel: true
          group: "Desktop"
          wait-on: "http://127.0.0.1:5001"
          config-file: cypress/config/desktop.config.js
          config: baseUrl=http://127.0.0.1:5001
        env:
          DATABASE_URL: postgresql://postgres:postgres@localhost/eventcally_tests
          TESTING: 1
          CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }}
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
