Example #1
0
 def test_init(self):
     resource = 'r123'
     expiration = 10
     policy = Policy(resource, expiration)
     self.assertEqual(policy.resource, resource)
     self.assertEqual(policy.expiration, expiration)
     self.assertEqual(policy.extensions, {})
Example #2
0
 def test_encode(self, base64, json):
     resource = 'r123'
     expiration = 10
     policy = Policy(resource, expiration)
     policy.extensions = {'remote_ip': '10.1.1.1'}
     encoded = policy.encode()
     json.encode.assert_called_once_with(
         {
             Policy.RESOURCE: policy.resource,
             Policy.EXPIRATION: policy.expiration,
             Policy.EXTENSIONS: policy.extensions
         })
     base64.encode.assert_called_once_with(json.encode.return_value)
     self.assertEqual(encoded, base64.encode.return_value)
Example #3
0
    def test_sign(self, encode, digest, base64):
        resource = 'r123'
        expiration = 10
        policy = Policy(resource, expiration)
        policy.extensions = {'remote_ip': '10.1.1.1'}
        key = Mock()

        # test
        signed = policy.sign(key)

        # validation
        digest.assert_called_once_with(encode.return_value)
        key.sign.assert_called_once_with(digest.return_value)
        base64.encode.assert_called_once_with(key.sign.return_value)
        self.assertEqual(signed, (encode.return_value, base64.encode.return_value))
Example #4
0
    def test_validate(self, digest, decode, base64, time):
        time.return_value = 10
        decode.return_value = Policy('', 20)
        key = Mock()
        encoded = '12345=='
        signature = '0x44'

        # test
        policy = Policy.validate(key, encoded, signature)

        # validation
        digest.assert_called_once_with(encoded)
        base64.decode.assert_called_once_with(signature)
        key.verify.assert_called_once_with(digest.return_value, base64.decode.return_value)
        decode.assert_called_once_with(encoded)
        self.assertEqual(policy, decode.return_value)
Example #5
0
 def test_str(self):
     policy = Policy('r123', 10)
     self.assertEqual(str(policy), str(policy.__dict__))
Example #6
0
 def test_validate_policy_expired(self, decode, time):
     time.return_value = 20
     decode.return_value = Policy('', 10)
     key = Mock()
     self.assertRaises(PolicyExpired, Policy.validate, key, '', '')