test_validate_service.py 2.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from __future__ import absolute_import
from .init import *

from django.test import RequestFactory

import os
import pytest
from lxml import etree
from cas_server.views import ValidateService
from cas_server import models

from .dummy import *

@pytest.mark.django_db
Valentin Samir's avatar
Valentin Samir committed
15
@dummy_ticket(models.ServiceTicket, 'https://www.example.com', "ST-random")
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
def test_validate_service_view_ok():
    factory = RequestFactory()
    request = factory.get('/serviceValidate?ticket=ST-random&service=https://www.example.com')

    request.session = DummySession()

    validate = ValidateService()
    validate.allow_proxy_ticket = False
    response = validate.get(request)

    assert response.status_code == 200

    root = etree.fromstring(response.content)
    users = root.xpath("//cas:user", namespaces={'cas': "http://www.yale.edu/tp/cas"})

    assert len(users) == 1
    assert users[0].text == "test"

    attributes = root.xpath("//cas:attributes", namespaces={'cas': "http://www.yale.edu/tp/cas"})

    assert len(attributes) == 1
    
    attrs = {}
    for attr in attributes[0]:
        attrs[attr.tag[len("http://www.yale.edu/tp/cas")+2:]]=attr.text

    assert 'mail' in attrs
    assert attrs['mail'] == 'test@example.com'



@pytest.mark.django_db
Valentin Samir's avatar
Valentin Samir committed
48
@dummy_ticket(models.ServiceTicket, 'https://www.example2.com', "ST-random")
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
def test_validate_service_view_badservice():
    factory = RequestFactory()
    request = factory.get('/serviceValidate?ticket=ST-random&service=https://www.example1.com')

    request.session = DummySession()

    validate = ValidateService()
    validate.allow_proxy_ticket = False
    response = validate.get(request)

    assert response.status_code == 200

    root = etree.fromstring(response.content)

    error = root.xpath("//cas:authenticationFailure", namespaces={'cas': "http://www.yale.edu/tp/cas"})
    
    assert len(error) == 1
    assert error[0].attrib['code'] == 'INVALID_SERVICE'

@pytest.mark.django_db
Valentin Samir's avatar
Valentin Samir committed
69
@dummy_ticket(models.ServiceTicket, 'https://www.example.com', "ST-random2")
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
def test_validate_service_view_badticket():
    factory = RequestFactory()
    request = factory.get('/serviceValidate?ticket=ST-random1&service=https://www.example.com')

    request.session = DummySession()

    validate = ValidateService()
    validate.allow_proxy_ticket = False
    response = validate.get(request)

    assert response.status_code == 200

    root = etree.fromstring(response.content)

    error = root.xpath("//cas:authenticationFailure", namespaces={'cas': "http://www.yale.edu/tp/cas"})
    
    assert len(error) == 1
    assert error[0].attrib['code'] == 'INVALID_TICKET'