test_signature_validator.py 3.71 KB
Newer Older
1 2 3 4
"""
Tests for the SignatureValidator class.
"""

5
import ddt
6 7 8 9 10 11 12 13
from django.test import TestCase
from django.test.client import RequestFactory
from mock import patch

from lti_provider.models import LtiConsumer
from lti_provider.signature_validator import SignatureValidator


14
def get_lti_consumer():
15
    """
16
    Helper method for all Signature Validator tests to get an LtiConsumer object.
17
    """
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    return LtiConsumer(
        consumer_name='Consumer Name',
        consumer_key='Consumer Key',
        consumer_secret='Consumer Secret'
    )


@ddt.ddt
class ClientKeyValidatorTest(TestCase):
    """
    Tests for the check_client_key method in the SignatureValidator class.
    """

    def setUp(self):
        super(ClientKeyValidatorTest, self).setUp()
        self.lti_consumer = get_lti_consumer()
34 35 36 37 38

    def test_valid_client_key(self):
        """
        Verify that check_client_key succeeds with a valid key
        """
39 40
        key = self.lti_consumer.consumer_key
        self.assertTrue(SignatureValidator(self.lti_consumer).check_client_key(key))
41

42 43 44 45 46 47 48
    @ddt.data(
        ('0123456789012345678901234567890123456789',),
        ('',),
        (None,),
    )
    @ddt.unpack
    def test_invalid_client_key(self, key):
49
        """
50
        Verify that check_client_key fails with a disallowed key
51
        """
52
        self.assertFalse(SignatureValidator(self.lti_consumer).check_client_key(key))
53 54


55 56 57 58 59 60 61 62 63
@ddt.ddt
class NonceValidatorTest(TestCase):
    """
    Tests for the check_nonce method in the SignatureValidator class.
    """

    def setUp(self):
        super(NonceValidatorTest, self).setUp()
        self.lti_consumer = get_lti_consumer()
64 65 66 67 68 69

    def test_valid_nonce(self):
        """
        Verify that check_nonce succeeds with a key of maximum length
        """
        nonce = '0123456789012345678901234567890123456789012345678901234567890123'
70
        self.assertTrue(SignatureValidator(self.lti_consumer).check_nonce(nonce))
71

72 73 74 75 76 77 78
    @ddt.data(
        ('01234567890123456789012345678901234567890123456789012345678901234',),
        ('',),
        (None,),
    )
    @ddt.unpack
    def test_invalid_nonce(self, nonce):
79
        """
80
        Verify that check_nonce fails with badly formatted nonce
81
        """
82
        self.assertFalse(SignatureValidator(self.lti_consumer).check_nonce(nonce))
83 84


85 86 87 88 89 90 91 92 93
class SignatureValidatorTest(TestCase):
    """
    Tests for the custom SignatureValidator class that uses the oauthlib library
    to check message signatures. Note that these tests mock out the library
    itself, since we assume it to be correct.
    """
    def setUp(self):
        super(SignatureValidatorTest, self).setUp()
        self.lti_consumer = get_lti_consumer()
94 95 96

    def test_get_existing_client_secret(self):
        """
97 98
        Verify that get_client_secret returns the right value for the correct
        key
99
        """
100 101 102
        key = self.lti_consumer.consumer_key
        secret = SignatureValidator(self.lti_consumer).get_client_secret(key, None)
        self.assertEqual(secret, self.lti_consumer.consumer_secret)
103 104 105 106 107 108 109 110 111 112 113 114

    @patch('oauthlib.oauth1.SignatureOnlyEndpoint.validate_request',
           return_value=(True, None))
    def test_verification_parameters(self, verify_mock):
        """
        Verify that the signature validaton library method is called using the
        correct parameters derived from the HttpRequest.
        """
        body = 'oauth_signature_method=HMAC-SHA1&oauth_version=1.0'
        content_type = 'application/x-www-form-urlencoded'
        request = RequestFactory().post('/url', body, content_type=content_type)
        headers = {'Content-Type': content_type}
115
        SignatureValidator(self.lti_consumer).verify(request)
116 117
        verify_mock.assert_called_once_with(
            request.build_absolute_uri(), 'POST', body, headers)