Пример #1
0
 def test_unlock_unexisting_lock_fails(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     # unlock!
     lr.delete({'token': 'anything'}, None)
     responder.send_response_json.assert_called_with(
         404, error='lock not found')
Пример #2
0
 def test_unlock_unexisting_lock_fails(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     # unlock!
     lr.delete({'token': 'anything'}, None)
     responder.send_response_json.assert_called_with(404,
                                                     error='lock not found')
Пример #3
0
 def test__try_obtain_filesystem_lock(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     self.assertFalse(lr._lock.locked)
     self.assertTrue(lr._try_obtain_filesystem_lock())
     self.assertTrue(lr._lock.locked)
     lr._try_release_filesystem_lock()
Пример #4
0
 def test__try_release_filesystem_lock(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     lr._try_obtain_filesystem_lock()
     self.assertTrue(lr._lock.locked)
     lr._try_release_filesystem_lock()
     self.assertFalse(lr._lock.locked)
Пример #5
0
 def test_unlock_with_wrong_token_fails(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     # lock!
     lr.put({}, None)
     # unlock!
     lr.delete({'token': 'wrongtoken'}, None)
     self.assertIsNotNone(lr._shared_db.get_doc('lock-uuid'))
     responder.send_response_json.assert_called_with(
         401, error='unlock unauthorized')
Пример #6
0
 def test_delete(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     # lock!
     lr.put({}, None)
     lock_doc = lr._shared_db.get_doc('lock-uuid')
     token = lock_doc.content[LockResource.LOCK_TOKEN_KEY]
     # unlock!
     lr.delete({'token': token}, None)
     self.assertFalse(lr._lock.locked)
     self.assertIsNone(lr._shared_db.get_doc('lock-uuid'))
     responder.send_response_json.assert_called_with(200)
Пример #7
0
 def test_put(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     # lock!
     lr.put({}, None)
     # assert lock document was correctly written
     lock_doc = lr._shared_db.get_doc('lock-uuid')
     self.assertIsNotNone(lock_doc)
     self.assertTrue(LockResource.TIMESTAMP_KEY in lock_doc.content)
     self.assertTrue(LockResource.LOCK_TOKEN_KEY in lock_doc.content)
     timestamp = lock_doc.content[LockResource.TIMESTAMP_KEY]
     token = lock_doc.content[LockResource.LOCK_TOKEN_KEY]
     self.assertTrue(timestamp < time.time())
     self.assertTrue(time.time() < timestamp + LockResource.TIMEOUT)
     # assert response to user
     responder.send_response_json.assert_called_with(
         201, token=token, timeout=LockResource.TIMEOUT)
Пример #8
0
 def test_put_while_locked_fails(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     # lock!
     lr.put({}, None)
     # try to lock again!
     lr.put({}, None)
     self.assertEqual(
         len(responder.send_response_json.call_args), 2)
     self.assertEqual(
         responder.send_response_json.call_args[0], (403,))
     self.assertEqual(
         len(responder.send_response_json.call_args[1]), 2)
     self.assertTrue(
         'remaining' in responder.send_response_json.call_args[1])
     self.assertTrue(
         responder.send_response_json.call_args[1]['remaining'] > 0)
Пример #9
0
 def test_put(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     # lock!
     lr.put({}, None)
     # assert lock document was correctly written
     lock_doc = lr._shared_db.get_doc('lock-uuid')
     self.assertIsNotNone(lock_doc)
     self.assertTrue(LockResource.TIMESTAMP_KEY in lock_doc.content)
     self.assertTrue(LockResource.LOCK_TOKEN_KEY in lock_doc.content)
     timestamp = lock_doc.content[LockResource.TIMESTAMP_KEY]
     token = lock_doc.content[LockResource.LOCK_TOKEN_KEY]
     self.assertTrue(timestamp < time.time())
     self.assertTrue(time.time() < timestamp + LockResource.TIMEOUT)
     # assert response to user
     responder.send_response_json.assert_called_with(
         201, token=token,
         timeout=LockResource.TIMEOUT)
Пример #10
0
 def test_unlock_with_wrong_token_fails(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     # lock!
     lr.put({}, None)
     # unlock!
     lr.delete({'token': 'wrongtoken'}, None)
     self.assertIsNotNone(lr._shared_db.get_doc('lock-uuid'))
     responder.send_response_json.assert_called_with(
         401, error='unlock unauthorized')
Пример #11
0
 def test_delete(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     # lock!
     lr.put({}, None)
     lock_doc = lr._shared_db.get_doc('lock-uuid')
     token = lock_doc.content[LockResource.LOCK_TOKEN_KEY]
     # unlock!
     lr.delete({'token': token}, None)
     self.assertFalse(lr._lock.locked)
     self.assertIsNone(lr._shared_db.get_doc('lock-uuid'))
     responder.send_response_json.assert_called_with(200)
Пример #12
0
 def test_put_while_locked_fails(self):
     responder = mock.Mock()
     lr = LockResource('uuid', self._state, responder)
     # lock!
     lr.put({}, None)
     # try to lock again!
     lr.put({}, None)
     self.assertEqual(len(responder.send_response_json.call_args), 2)
     self.assertEqual(responder.send_response_json.call_args[0], (403, ))
     self.assertEqual(len(responder.send_response_json.call_args[1]), 2)
     self.assertTrue(
         'remaining' in responder.send_response_json.call_args[1])
     self.assertTrue(
         responder.send_response_json.call_args[1]['remaining'] > 0)