def testPost_Admin_RecountThenReset(self): """Test private reset method.""" # Create a vote and trigger a recount on the blockable to update the score. test_utils.CreateVote(self.santa_blockable) self.santa_blockable.put() # Ensure Vote properly updated the blockable score. with self.LoggedInUser(admin=True): response = self.testapp.get(self.ROUTE % self.santa_blockable.key.id()) output = response.json self.assertEqual(output['id'], self.santa_blockable.key.id()) self.assertEqual(output['score'], 1) # Issue a reset and ensure the resulting score is 0. params = {'reset': 'reset'} response = self.testapp.post( self.ROUTE % self.santa_blockable.key.id(), params) output = response.json self.assertEqual(output['id'], self.santa_blockable.key.id()) self.assertEqual(output['score'], 0) self.assertBigQueryInsertions( [constants.BIGQUERY_TABLE.BINARY, constants.BIGQUERY_TABLE.RULE])
def testUserGetListOwnEventsWithContext(self): """Normal user getting list of their events with context.""" # Create a vote for event 1. test_utils.CreateVote( self.santa_event1_from_user2.blockable_key.get(), user_email=self.santa_event2_from_user1.user_key.id(), was_yes_vote=False) params = {'withContext': 'true'} with self.LoggedInUser(user=self.user_1): response = self.testapp.get(self.ROUTE, params) content = response.json['content'] self.assertEqual(4, len(content)) self.assertFalse(response.json['more']) event1 = [ dict_ for dict_ in content if dict_['blockable']['id'] == self.santa_blockable1.key.id() ][0] self.assertEqual(len(event1.keys()), 5) self.assertEqual(self.santa_event2_from_user1.host_id, event1['host']['id']) blockable_key = ndb.Key(urlsafe=event1['event']['blockableKey']) self.assertEqual(self.santa_event2_from_user1.blockable_key, blockable_key) self.assertEqual(event1['blockable']['id'], blockable_key.id()) self.assertFalse(event1['vote']['wasYesVote']) event2 = [ dict_ for dict_ in content if dict_['blockable']['id'] == self.santa_blockable2.key.id() ][0] self.assertIsNone(event2['vote'])
def testUserGetOwnEventWithContext_Bundle(self): """Getting an event of the requesting user's by id.""" bundle = test_utils.CreateSantaBundle( bundle_binaries=[self.santa_blockable1]) event = test_utils.CreateSantaEvent( self.santa_blockable1, bundle_key=bundle.key, executing_user=self.user_1.nickname, event_type=constants.EVENT_TYPE.ALLOW_UNKNOWN, host_id=self.santa_host1.key.id(), last_blocked_dt=datetime.datetime(2015, 3, 1, 1, 0, 0), first_blocked_dt=datetime.datetime(2015, 3, 1, 1, 0, 0), parent=utils.ConcatenateKeys(self.user_1.key, self.santa_host1.key, bundle.key)) test_utils.CreateVote(bundle, user_email=self.user_1.email) params = {'withContext': 'true'} with self.LoggedInUser(user=self.user_1): response = self.testapp.get(self.ROUTE % event.key.urlsafe(), params) output = response.json self.assertIn('application/json', response.headers['Content-type']) self.assertIsInstance(output, dict) self.assertEqual(output['event']['id'], event.key.id()) self.assertEqual(output['host']['id'], output['event']['hostId']) bundle_key = ndb.Key(urlsafe=output['event']['bundleKey']) self.assertEqual(output['blockable']['id'], bundle_key.id()) self.assertIsNotNone(output['vote'])
def testBlockableKey_BadKey(self): vote = test_utils.CreateVote(self.blockable, user_email=self.user.email) # Take out User key section. vote.key = utils.ConcatenateKeys(self.blockable.key, ndb.Key(base.Vote, vote.key.id())) self.assertIsNone(vote.blockable_key)
def testIgnoreCalculateScoreBeforeUpload(self): bundle = test_utils.CreateSantaBundle(uploaded_dt=None) test_utils.CreateVote(bundle) # Trigger the SantaBundle.score ComputedProperty calculation. bundle.put() # The score should have not reflected the real score until the bundle is # uploaded. self.assertEqual(0, bundle.key.get().score)
def testGetStrongestVote_Downvote(self): self.assertIsNone(self.blockable_1.GetStrongestVote()) for weight in [-6, -1, -1, 1, 2]: test_utils.CreateVote(self.blockable_1, weight=weight) vote = self.blockable_1.GetStrongestVote() self.assertIsNotNone(vote) self.assertEqual(-6, vote.weight)
def testBlockableKey_MultiPartKey(self): vote = test_utils.CreateVote(self.blockable, user_email=self.user.email) # Add another test_blockable key to simulate a length-two blockable key. vote.key = utils.ConcatenateKeys( self.blockable.key, base.Vote.GetKey(self.blockable.key, self.user.key)) self.assertIsNotNone(vote.blockable_key) self.assertEqual(2, len(vote.blockable_key.pairs())) self.assertEqual(self.blockable.key, vote.blockable_key.parent())
def setUp(self): app = webapp2.WSGIApplication(routes=[votes.ROUTES]) super(VotesTest, self).setUp(wsgi_app=app) self.santa_blockable = test_utils.CreateSantaBlockable() self.other_blockable = test_utils.CreateSantaBlockable() self.santa_certificate = test_utils.CreateSantaCertificate() self.user_1 = test_utils.CreateUser() self.user_2 = test_utils.CreateUser() self.vote_1 = test_utils.CreateVote( self.santa_blockable, user_email=self.user_1.email, weight=2) self.vote_2 = test_utils.CreateVote( self.other_blockable, user_email=self.user_1.email, weight=10) self.vote_3 = test_utils.CreateVote( self.santa_certificate, user_email=self.user_1.email, weight=0, candidate_type='CERTIFICATE') self.PatchValidateXSRFToken()
def testToDict_Score(self): blockable = test_utils.CreateBlockable() test_utils.CreateVote(blockable) # Recalculate the 'score' property blockable.put() # Mock out the blockable's _CalculateScore function. with mock.patch.object( blockable._properties['score'], '_func') as calc_mock: # pylint: disable=protected-access blockable_dict = blockable.to_dict() self.assertFalse(calc_mock.called) self.assertIn('score', blockable_dict) self.assertEqual(1, blockable_dict['score'])
def testSetKey_NotInEffect(self): expected_key = ndb.Key(flat=(self.blockable.key.flat() + self.user.key.flat() + ('Vote', None))) key = base.Vote.GetKey(self.blockable.key, self.user.key, in_effect=False) self.assertEqual(expected_key, key) # Putting the vote results in a random ID being generated. vote = test_utils.CreateVote(self.blockable) vote.key = key vote.put() self.assertIsNotNone(vote.key.id())
def testUserGetListOwnEventsWithBlockableAndContext(self): """Normal user getting list of their events with context, by blockable.""" test_utils.CreateVote(self.santa_blockable1, user_email=self.user_1.email) test_utils.CreateVote(self.santa_blockable1, user_email=self.user_2.email) params = { 'blockableKey': self.santa_blockable1.key.urlsafe(), 'withContext': 'true' } with self.LoggedInUser(user=self.user_1): response = self.testapp.get(self.ROUTE, params) output = response.json self.assertIn('application/json', response.headers['Content-type']) self.assertIsInstance(output, dict) self.assertLen(output['content'], 2) event_with_context = output['content'][0] self.assertLen(event_with_context.keys(), 5) self.assertEqual(event_with_context['host']['id'], event_with_context['event']['hostId']) blockable_key = ndb.Key( urlsafe=event_with_context['event']['blockableKey']) self.assertEqual(event_with_context['blockable']['id'], blockable_key.id()) self.assertEqual(event_with_context['blockable']['fileName'], event_with_context['event']['fileName']) self.assertEqual(event_with_context['cert']['id'], event_with_context['blockable']['certId']) self.assertEqual( user_utils.EmailToUsername( event_with_context['vote']['userEmail']), event_with_context['event']['executingUser'])
def testResetBlockable(self): """Test private reset method.""" # Create a vote and trigger a recount on the blockable to update the score. test_utils.CreateVote(self.santa_blockable) self.santa_blockable.put() # Ensure Vote properly updated the blockable score. with self.LoggedInUser(admin=True): response = self.testapp.get('/%s' % self.santa_blockable.key.id()) output = response.json self.assertEqual(output['id'], self.santa_blockable.key.id()) self.assertEqual(output['score'], 1) # Issue a reset and ensure the resulting score is 0. params = {'reset': 'reset'} response = self.testapp.post('/%s' % self.santa_blockable.key.id(), params) output = response.json self.assertEqual(output['id'], self.santa_blockable.key.id()) self.assertEqual(output['score'], 0)
def testInEffect(self): vote = test_utils.CreateVote(self.blockable) self.assertTrue(vote.in_effect) vote.key = None self.assertFalse(vote.in_effect)
def testUserKey(self): vote = test_utils.CreateVote(self.blockable, user_email=self.user.email) self.assertEqual(self.user.key, vote.user_key)
def testBlockableKey_NoKey(self): vote = test_utils.CreateVote(self.blockable, user_email=self.user.email) vote.key = None self.assertIsNone(vote.blockable_key)
def testBlockableKey(self): vote = test_utils.CreateVote(self.blockable, user_email=self.user.email) vote.key = base.Vote.GetKey(self.blockable.key, self.user.key) self.assertEqual(self.blockable.key, vote.blockable_key)