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')
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')
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()
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)
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')
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)
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)
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)
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)