Exemplo n.º 1
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)
Exemplo n.º 2
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))
Exemplo n.º 3
0
 def test_decode(self, base62, json, load):
     encoded = '1234'
     decoded = Policy.decode(encoded)
     base62.decode.assert_called_once_with(encoded)
     json.decode.assert_called_once_with(base62.decode.return_value)
     load.assert_called_once_with(json.decode.return_value)
     self.assertEqual(decoded, load.return_value)
Exemplo n.º 4
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, {})
Exemplo n.º 5
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)
Exemplo n.º 6
0
 def test_decode(self, base62, json, load):
     encoded = '1234'
     decoded = Policy.decode(encoded)
     base62.decode.assert_called_once_with(encoded)
     json.decode.assert_called_once_with(base62.decode.return_value)
     load.assert_called_once_with(json.decode.return_value)
     self.assertEqual(decoded, load.return_value)
Exemplo n.º 7
0
 def test_load(self):
     policy = {
         Policy.RESOURCE: 'dog',
         Policy.EXPIRATION: 10,
         Policy.EXTENSIONS: {'remote_ip': '63.10.133'}
     }
     loaded = Policy.load(policy)
     self.assertTrue(isinstance(loaded, Policy))
     self.assertEqual(loaded.resource, policy[Policy.RESOURCE])
     self.assertEqual(loaded.expiration, policy[Policy.EXPIRATION])
     self.assertEqual(loaded.extensions, policy[Policy.EXTENSIONS])
Exemplo n.º 8
0
 def test_str(self):
     policy = Policy('r123', 10)
     self.assertEqual(str(policy), str(policy.__dict__))
Exemplo n.º 9
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, '', '')
Exemplo n.º 10
0
 def test_digest(self, sha256):
     p = '1234'
     digest = Policy.digest(p)
     sha256.assert_called_once_with()
     sha256.return_value.update.assert_called_once_with(p)
     self.assertEqual(digest, sha256.return_value.hexdigest.return_value)
Exemplo n.º 11
0
 def test_digest(self, sha256):
     p = '1234'
     digest = Policy.digest(p)
     sha256.assert_called_once_with()
     sha256.return_value.update.assert_called_once_with(p)
     self.assertEqual(digest, sha256.return_value.hexdigest.return_value)