def test_count_with_voter_device_id(self): """ Test the various cookie states :return: """ ####################################### # Generate the voter_device_id cookie response0 = self.client.get(self.generate_voter_device_id_url) json_data0 = json.loads(response0.content.decode()) # Make sure we got back a voter_device_id we can use self.assertEqual('voter_device_id' in json_data0, True, "voter_device_id expected in the deviceIdGenerateView json response") # Now put the voter_device_id in a variable we can use below voter_device_id = json_data0['voter_device_id'] if 'voter_device_id' in json_data0 else '' ####################################### # Test for status: VOTER_CREATED response02 = self.client.get(self.voter_create_url) json_data02 = json.loads(response02.content.decode()) self.assertEqual('status' in json_data02, True, "status expected in the voterCreateView json response but not found") self.assertEqual('voter_device_id' in json_data02, True, "voter_device_id expected in the voterCreateView json response but not found") # With a brand new voter_device_id, a new voter record should be created self.assertEqual( json_data02['status'], 'VOTER_CREATED', "status: {status} (VOTER_CREATED expected), voter_device_id: {voter_device_id}".format( status=json_data02['status'], voter_device_id=json_data02['voter_device_id'])) ####################################### # Check to see if there is 1 voter - i.e., the viewer response11 = self.client.get(self.voter_count_url) json_data11 = json.loads(response11.content.decode()) self.assertEqual('success' in json_data11, True, "'success' expected in the json response, and not found") self.assertEqual('voter_count' in json_data11, True, "'voter_count' expected in the voterCount json response") self.assertEqual( json_data11['voter_count'], 1, "success: {success} (voter_count '1' expected), voter_count: {voter_count}".format( success=json_data11['success'], voter_count=json_data11['voter_count'])) ####################################### # Add 3 voters so we can check count again voter_manager = VoterManager() email1 = "*****@*****.**" voter_manager.create_voter( email=email1, password="******", ) email2 = "*****@*****.**" voter_manager.create_voter( email=email2, password="******", ) email3 = "*****@*****.**" voter_manager.create_voter( email=email3, password="******", ) ####################################### # Check to see if there are 4 voters response12 = self.client.get(self.voter_count_url) json_data12 = json.loads(response12.content.decode()) self.assertEqual('success' in json_data12, True, "'success' expected in the json response, and not found") self.assertEqual('voter_count' in json_data12, True, "'voter_count' expected in the voterCount json response") self.assertEqual( json_data12['voter_count'], 4, "success: {success} (voter_count '4' expected), voter_count: {voter_count}".format( success=json_data12['success'], voter_count=json_data12['voter_count']))
def facebook_sign_in_for_api(voter_device_id, facebook_id=None, facebook_email=None): # facebookSignIn """ :param voter_device_id: :return: """ status = "" success = False # Get voter_id from the voter_device_id results = is_voter_device_id_valid(voter_device_id) if not results['success']: results = { 'success': False, 'status': "VALID_VOTER_DEVICE_ID_MISSING", 'voter_device_id': voter_device_id, 'facebook_id': facebook_id, 'facebook_email': facebook_email, } return results voter_manager = VoterManager() results = voter_manager.retrieve_voter_from_voter_device_id( voter_device_id) if not positive_value_exists(results['voter_found']): results = { 'success': False, 'status': "VALID_VOTER_MISSING", 'voter_device_id': voter_device_id, 'facebook_id': facebook_id, 'facebook_email': facebook_email, } return results voter = results['voter'] results_from_facebook_id = voter_manager.retrieve_voter_by_facebook_id( facebook_id) if positive_value_exists(results_from_facebook_id['voter_found']): voter_found_with_facebook_id = results_from_facebook_id['voter'] if voter_found_with_facebook_id.id == voter.id: # If here, the owner of the facebook_id is already the current primary voter status += "FACEBOOK_SIGN_IN-ALREADY_LINKED_TO_THIS_FACEBOOK_ACCOUNT " success = True # Only save if the email is different than what is saved if positive_value_exists(facebook_email) or facebook_email == '': results = voter_manager.save_facebook_user_values( voter, facebook_id, facebook_email) status += results['status'] success = results['success'] else: # If here, we need to merge accounts TODO # ...but for now we are simply going to switch to the earlier account and abandon # the newer account if positive_value_exists(facebook_email) or facebook_email == '': results = voter_manager.save_facebook_user_values( voter_found_with_facebook_id, facebook_id, facebook_email) status += results['status'] + ", " success = results['success'] # Relink this voter_device_id to the original account voter_device_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_manager.retrieve_voter_device_link( voter_device_id) voter_device_link = voter_device_link_results['voter_device_link'] update_voter_device_link_results = voter_device_manager.update_voter_device_link( voter_device_link, voter_found_with_facebook_id) if update_voter_device_link_results['voter_device_link_updated']: status += "FACEBOOK_SIGN_IN-ALREADY_LINKED_TO_OTHER_ACCOUNT-TRANSFERRED " success = True else: status = "FACEBOOK_SIGN_IN-ALREADY_LINKED_TO_OTHER_ACCOUNT-COULD_NOT_TRANSFER " success = False else: # An existing account linked to this facebook account was not found results = voter_manager.save_facebook_user_values( voter, facebook_id, facebook_email) status = results['status'] success = results['success'] if success: results = { 'success': True, 'status': status, 'voter_device_id': voter_device_id, 'facebook_id': facebook_id, 'facebook_email': facebook_email, } else: results = { 'success': False, 'status': status, 'voter_device_id': voter_device_id, 'facebook_id': facebook_id, 'facebook_email': facebook_email, } return results
def test_count_with_no_voter_device_id(self): """ This API should work even if person isn't signed in :return: """ ####################################### # Check to see if there are 0 voters response = self.client.get(self.voter_count_url) json_data = json.loads(response.content.decode()) # Python3 solution? Problem is refused connection # req = Request(self.voter_count_url) # response = urlopen(req) # json_data = response.read() self.assertEqual('success' in json_data, True, "'success' expected in the json response, and not found") self.assertEqual('voter_count' in json_data, True, "'voter_count' expected in the voterCount json response") self.assertEqual( json_data['voter_count'], 0, "success: {success} (voter_count '0' expected), voter_count: {voter_count}".format( success=json_data['success'], voter_count=json_data['voter_count'])) ####################################### # Add 3 voters so we can check count again voter_manager = VoterManager() email1 = "*****@*****.**" voter_manager.create_voter( email=email1, password="******", ) email2 = "*****@*****.**" voter_manager.create_voter( email=email2, password="******", ) email3 = "*****@*****.**" voter_manager.create_voter( email=email3, password="******", ) ####################################### # Check to see if there are 3 voters response2 = self.client.get(self.voter_count_url) json_data2 = json.loads(response2.content.decode()) self.assertEqual('success' in json_data2, True, "'success' expected in the json response, and not found") self.assertEqual('voter_count' in json_data2, True, "'voter_count' expected in the voterCount json response") self.assertEqual( json_data2['voter_count'], 3, "success: {success} (voter_count '3' expected), voter_count: {voter_count}".format( success=json_data2['success'], voter_count=json_data2['voter_count'])) ####################################### # Remove data for 3 voters voter_manager.delete_voter(email1) voter_manager.delete_voter(email2) voter_manager.delete_voter(email3) ####################################### # Check to see if there are 0 voters response3 = self.client.get(self.voter_count_url) json_data3 = json.loads(response3.content.decode()) self.assertEqual('success' in json_data, True, "'success' expected in the json response, and not found") self.assertEqual('voter_count' in json_data3, True, "'voter_count' expected in the voterCount json response") self.assertEqual( json_data3['voter_count'], 0, "success: {success} (voter_count '0' expected - 2nd pass), voter_count: {voter_count}".format( success=json_data3['success'], voter_count=json_data3['voter_count']))
def voter_we_vote_id(self): voter_manager = VoterManager() return voter_manager.fetch_we_vote_id_from_local_id(self.voter_id)