name: Tests CI

on:
  push:
    branches:
      - "*"
  pull_request:
    branches:
      - "*"

jobs:
  test:
    runs-on: ubuntu-latest
    name: Tests
    strategy:
      matrix:
        python-version: ["3.11.8"]

    steps:
      - uses: actions/checkout@v4

      - uses: harmon758/postgresql-action@v1
        with:
          postgresql version: "15"
          postgresql db: "pipeline"
          postgresql user: "pipeline"
          postgresql password: "pipeline123456"

      - name: Setup Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
          check-latest: true

      - name: Install redis
        run: |
          sudo apt update
          sudo apt install -y redis
          redis-server --version

      - name: Install requirements
        working-directory: api/tacticalrmm
        run: |
          python --version
          SETTINGS_FILE="tacticalrmm/settings.py"
          SETUPTOOLS_VER=$(grep "^SETUPTOOLS_VER" "$SETTINGS_FILE" | awk -F'[= "]' '{print $5}')
          WHEEL_VER=$(grep "^WHEEL_VER" "$SETTINGS_FILE" | awk -F'[= "]' '{print $5}')
          pip install --upgrade pip
          pip install setuptools==${SETUPTOOLS_VER} wheel==${WHEEL_VER}
          pip install -r requirements.txt -r requirements-test.txt

      - name: Codestyle black
        working-directory: api
        run: |
          black --exclude migrations/ --check tacticalrmm
          if [ $? -ne 0 ]; then
              exit 1
          fi

      - name: Lint with flake8
        working-directory: api/tacticalrmm
        run: |
          flake8 --config .flake8 .
          if [ $? -ne 0 ]; then
              exit 1
          fi

      - name: Run django tests
        env:
          GHACTIONS: "yes"
        working-directory: api/tacticalrmm
        run: |
          pytest
          if [ $? -ne 0 ]; then
              exit 1
          fi

      - uses: codecov/codecov-action@v3
        with:
          directory: ./api/tacticalrmm
          files: ./api/tacticalrmm/coverage.xml
          verbose: true
