test_views_auth.py 1.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
from __future__ import absolute_import
from .init import *

from django.test import RequestFactory

import os
import pytest

from cas_server.views import Auth
from cas_server import models

from .dummy import *

settings.CAS_AUTH_SHARED_SECRET = "test"

@pytest.mark.django_db
def test_auth_view_goodpass():
    factory = RequestFactory()
    request = factory.post('/auth', {'username':'test', 'password':'test', 'service':'https://www.example.com', 'secret':'test'})

    request.session = DummySession()

    models.User.objects = DummyUserManager(username="test", session_key=request.session.session_key)
    models.ServiceTicket.objects = DummyTicketManager(models.ServiceTicket, 'https://www.example.com', "ST-random")
    models.ServicePattern.validate = classmethod(lambda x,y: models.ServicePattern())

    auth = Auth()
    response = auth.post(request)

    assert response.status_code == 200
    assert response.content == "yes\n"


def test_auth_view_badpass():
    factory = RequestFactory()
    request = factory.post('/auth', {'username':'test', 'password':'badpass', 'service':'https://www.example.com', 'secret':'test'})

    request.session = DummySession()

    models.User.objects = DummyUserManager(username="test", session_key=request.session.session_key)
    models.ServiceTicket.objects = DummyTicketManager(models.ServiceTicket, 'https://www.example.com', "ST-random")
    models.ServicePattern.validate = classmethod(lambda x,y: models.ServicePattern())

    auth = Auth()
    response = auth.post(request)

    assert response.status_code == 200
    assert response.content == "no\n"