# Create your tests here.

# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import time
import os
from io import StringIO
from urllib.parse import urlparse, urlunparse

import requests

from django.core import mail
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.urls import reverse
from django.test import TestCase, Client, override_settings
from django.utils.http import urlencode
from django.contrib.auth import authenticate
from django.conf import settings


from oauth2_provider.models import get_application_model


def register_util(t: TestCase, username: str):
    t.password = "asdklfja;ldkfja;df"
    t.username = username
    t.email = f"{t.username}@example.org"
    t.user = get_user_model().objects.create(
        username=t.username,
        email=t.email,
    )
    t.user.set_password(t.password)
    t.user.save()


def login_util(t: TestCase, c: Client, redirect_to: str):
    payload = {
        "login": t.username,
        "password": t.password,
    }
    resp = c.post(reverse("accounts.login"), payload)
    t.assertEqual(resp.status_code, 302)
    t.assertEqual(resp.headers["location"], reverse(redirect_to))


class CreateOidCApplicaiton(TestCase):
    """
    Test command: manage.py create_oidc
    """

    def setUp(self):
        self.username = "oidcadmin"
        register_util(t=self, username=self.username)

    def test_cmd(self):

        Application = get_application_model()

        stdout = StringIO()
        stderr = StringIO()

        redirect_uri = "http://example.org"
        app_name = "test_cmd_oidc"

        # username exists
        call_command(
            "create_oidc",
            app_name,
            self.username,
            redirect_uri,
            stdout=stdout,
            stderr=stderr,
        )
        out = stdout.getvalue()

        self.assertIn(f"New application {app_name} created successfully", out)

        client_id = out.split("\n")[1].split(" ")[1]

        self.assertEqual(
            get_application_model().objects.filter(client_id=client_id).exists(), True
        )
        app = get_application_model().objects.get(name=app_name)
        self.assertEqual(app.client_id, client_id)
        self.assertEqual(app.name, app_name)
        self.assertEqual(app.user_id, self.user.id)
        self.assertEqual(app.redirect_uris, redirect_uri)
        self.assertEqual(app.skip_authorization, True)
        self.assertEqual(app.client_type, "confidential")
        self.assertEqual(app.authorization_grant_type, "authorization-code")
        self.assertEqual(app.algorithm, "HS256")