from django.test import TestCase, Client
from django.contrib.auth import get_user_model
from core.models import Plan
from django.urls import reverse


class AdminSiteTests(TestCase):

    def setUp(self):
        self.client = Client()
        self.admin_user = get_user_model().objects.create_superuser(
            username='admin_username',
            password='password123'
        )
        self.client.force_login(self.admin_user)
        self.plan = Plan(
            name='Light',
            price='10'
        )
        self.plan.save()
        self.user = get_user_model().objects.create_user(
            username='user_username',
            password='aadd123',
            name='Jack',
            plan=self.plan
        )

    def test_users_listed(self):
        """Tests that users are listed on user page"""
        url = reverse('admin:core_user_changelist')
        res = self.client.get(url)

        self.assertContains(res, self.user.name)
        self.assertContains(res, self.user.plan)

    def test_user_change_page(self):
        """Test that the user edit page works"""
        url = reverse('admin:core_user_change', args=[self.user.id])
        # Works like: /admin/core/user/{id}
        res = self.client.get(url)

        self.assertEqual(res.status_code, 200)

    def test_create_user_page(self):
        """Test that the create user page works"""
        url = reverse('admin:core_user_add')
        res = self.client.get(url)

        self.assertEqual(res.status_code, 200)
