'''Integration test for app.py'''

import pytest
from dotenv import load_dotenv, find_dotenv
from todo_app.app import create_app
import os
from todo_app import trello_api_calls
from datetime import datetime
from bson.objectid import ObjectId

@pytest.fixture
def client():
    file_path = find_dotenv('.env.test')
    load_dotenv(file_path, override = True)
    test_app = create_app()
    test_app.config['LOGIN_DISABLED'] = True
    with test_app.test_client() as client:
        yield client

class MockResponse():
    def __init__(self):
        self.resp = [{
            "Title": "This is the title of test card",
            "_id": ObjectId("611faaa3920a094008f34e8c"),
            "Status": "Todo, doing and done",
            "UpdatedTime": datetime.now()
            }]
    def find(self, *args, **kwargs):
        return self.resp


@pytest.fixture
def mock_get_requests(monkeypatch):
    def mock_mongo(*args, **kwargs):
        return MockResponse()
    monkeypatch.setattr(trello_api_calls, 'establish_mongo_connection', mock_mongo)


def test_index_page(mock_get_requests, client):
    response = client.get('/')
    assert response.status_code == 200

    assert (b'This is the title of test card' in response.get_data()) == True
    assert (response.get_data().count(b'Mark As Complete')) == 2