def test_update_basket_task(self, mock_basket): # When a user is created or added to a group, the appropriate # calls to update basket are made email = '*****@*****.**' token = 'footoken' mock_basket.lookup_user.return_value = { 'email': email, } mock_basket.subscribe.return_value = { 'token': token, } country = CountryFactory.create(name='Greece', code='gr') city = CityFactory.create(name='Athens', country=country) user = UserFactory.create( email=email, userprofile={'geo_country': country, 'geo_city': city}) mock_basket.subscribe.reset_mock() # forget that subscribe was called group = GroupFactory.create(name='Web Development', functional_area=True) GroupFactory.create(name='Marketing', functional_area=True) data = {'country': 'gr', 'city': 'Athens', 'WEB_DEVELOPMENT': 'Y', 'MARKETING': 'N'} group.add_member(user.userprofile) # We just added a group, we should not need to subscribe anything ok_(not mock_basket.subscribe.called) # But we do need to update their phonebook record mock_basket.request.assert_called_with( 'post', 'custom_update_phonebook', token=token, data=data)
def setUp(self): self.user = UserFactory.create(email='*****@*****.**', userprofile={ 'geo_country': None, 'geo_region': None, 'geo_city': None }) self.data = { 'full_name': self.user.userprofile.full_name, 'email': self.user.email, 'username': self.user.username, 'lat': 40.005814, 'lng': -3.42071, 'externalaccount_set-MAX_NUM_FORMS': '1000', 'externalaccount_set-INITIAL_FORMS': '0', 'externalaccount_set-TOTAL_FORMS': '0', 'language_set-MAX_NUM_FORMS': '1000', 'language_set-INITIAL_FORMS': '0', 'language_set-TOTAL_FORMS': '0', 'location_section': '' } self.country = CountryFactory.create(mapbox_id='country1', name='Petoria') self.region = RegionFactory.create(country=self.country, mapbox_id='reg1', name='Ontario') self.city = CityFactory.create(region=self.region, mapbox_id='city1', name='Toronto')
def test_transform_city(self): city = CityFactory.create(name='LA') user = UserFactory.create(userprofile={'geo_city': city}) data = UserProfileDetailedSerializer(user.userprofile).data city = {'value': 'LA', 'privacy': 'Mozillians'} eq_(data['city'], city)
def test_location_city_region_optout(self, mock_reverse_geocode): country = CountryFactory.create(mapbox_id='country1', name='Petoria') region = RegionFactory.create(country=country, mapbox_id='region1', name='Ontario') city = CityFactory.create(region=region, mapbox_id='city1', name='Toronto') mock_reverse_geocode.return_value = (country, region, city) user = UserFactory.create(email='*****@*****.**') data = {'full_name': user.userprofile.full_name, 'email': user.email, 'username': user.username, 'lat': 40.005814, 'lng': -3.42071, 'externalaccount_set-MAX_NUM_FORMS': '1000', 'externalaccount_set-INITIAL_FORMS': '0', 'externalaccount_set-TOTAL_FORMS': '0', 'language_set-MAX_NUM_FORMS': '1000', 'language_set-INITIAL_FORMS': '0', 'language_set-TOTAL_FORMS': '0', } data.update(_get_privacy_fields(MOZILLIANS)) form = ProfileForm(data=data) eq_(form.is_valid(), True) eq_(form.instance.geo_country, country) eq_(form.instance.geo_region, None) eq_(form.instance.geo_city, None)
def test_transform_city(self): city = CityFactory.create(name='LA') user = UserFactory.create(userprofile={'geo_city': city}) context = {'request': self.factory.get('/')} serializer = UserProfileDetailedSerializer(user.userprofile, context=context) city = {'value': 'LA', 'privacy': 'Mozillians'} eq_(serializer.data['city'], city)
def test_query_with_space(self): city = CityFactory.create(name='Mountain View') user = UserFactory.create(userprofile={'geo_city': city}) client = Client() url = urlparams(self.mozilla_resource_url, city='mountain view') request = client.get(url, follow=True) data = json.loads(request.content) eq_(len(data['objects']), 1) eq_(data['objects'][0]['id'], user.userprofile.id)
def test_search_city(self): city = CityFactory.create(name=u"αθήνα") user = UserFactory.create(userprofile={"geo_city": city}) url = urlparams(self.mozilla_resource_url, city=user.userprofile.geo_city.name) client = Client() response = client.get(url, follow=True) data = json.loads(response.content) eq_(len(data["objects"]), 1) eq_(data["objects"][0]["id"], user.userprofile.id)
def test_query_with_space(self): city = CityFactory.create(name="Mountain View") user = UserFactory.create(userprofile={"geo_city": city}) client = Client() url = urlparams(self.mozilla_resource_url, city="mountain view") request = client.get(url, follow=True) data = json.loads(request.content) eq_(len(data["objects"]), 1) eq_(data["objects"][0]["id"], user.userprofile.id)
def test_search_city(self): city = CityFactory.create(name=u'αθήνα') user = UserFactory.create(userprofile={'geo_city': city}) url = urlparams(self.mozilla_resource_url, city=user.userprofile.geo_city.name) client = Client() response = client.get(url, follow=True) data = json.loads(response.content) eq_(len(data['objects']), 1) eq_(data['objects'][0]['id'], user.userprofile.id)
def test_deduplication_required(self): city = CityFactory.create() dup_city = CityFactory.create() result = { 'city': { 'name': dup_city.name, 'id': city.mapbox_id, 'lat': dup_city.lat, 'lon': dup_city.lng, } } result_to_city(result, dup_city.country, dup_city.region) lookup_args = { 'region': dup_city.region, 'country': dup_city.country, 'name': dup_city.name, } ok_(City.objects.filter(**lookup_args).exists()) ok_(not City.objects.filter(mapbox_id=dup_city.mapbox_id).exists())
def test_update_name(self): city = CityFactory.create() new_name = 'New %s' % city.name result = { 'city': { 'name': new_name, 'id': city.mapbox_id, 'lat': city.lat, 'lon': city.lng, } } result_to_city(result, city.country, None) city = City.objects.get(pk=city.pk) eq_(new_name, city.name)
def test_extract_document(self): country = CountryFactory.create(name='Greece', code='gr') region = RegionFactory.create(name='attika', country=country) city = CityFactory.create(name='athens', region=region, country=country) user = UserFactory.create( userprofile={ 'geo_city': city, 'geo_region': region, 'allows_community_sites': False, 'allows_mozilla_sites': False, 'geo_country': country, 'full_name': 'Nikos Koukos', 'bio': 'This is my bio' }) profile = user.userprofile group_1 = GroupFactory.create() group_2 = GroupFactory.create() skill_1 = SkillFactory.create() skill_2 = SkillFactory.create() LanguageFactory.create(code='fr', userprofile=profile) LanguageFactory.create(code='en', userprofile=profile) group_1.add_member(profile) group_2.add_member(profile) profile.skills.add(skill_1) profile.skills.add(skill_2) result = UserProfile.extract_document(user.userprofile.id) ok_(isinstance(result, dict)) eq_(result['id'], profile.id) eq_(result['is_vouched'], profile.is_vouched) eq_(result['region'], region.name) eq_(result['city'], city.name) eq_(result['allows_community_sites'], profile.allows_community_sites) eq_(result['allows_mozilla_sites'], profile.allows_mozilla_sites) eq_(set(result['country']), set(['gr', 'Greece'])) eq_(result['fullname'], profile.full_name.lower()) eq_(result['name'], profile.full_name.lower()) eq_(result['bio'], profile.bio) eq_(result['has_photo'], False) eq_(result['groups'], [group_1.name, group_2.name]) eq_(result['skills'], [skill_1.name, skill_2.name]) eq_(set(result['languages']), set([u'en', u'fr', u'english', u'french', u'français']))
def setUp(self): self.user = UserFactory.create(email='*****@*****.**') self.data = { 'full_name': self.user.userprofile.full_name, 'email': self.user.email, 'username': self.user.username, 'lat': 40.005814, 'lng': -3.42071, 'externalaccount_set-MAX_NUM_FORMS': '1000', 'externalaccount_set-INITIAL_FORMS': '0', 'externalaccount_set-TOTAL_FORMS': '0', 'language_set-MAX_NUM_FORMS': '1000', 'language_set-INITIAL_FORMS': '0', 'language_set-TOTAL_FORMS': '0', } self.country = CountryFactory.create(mapbox_id='country1', name='Petoria') self.region = RegionFactory.create(country=self.country, mapbox_id='reg1', name='Ontario') self.city = CityFactory.create(region=self.region, mapbox_id='city1', name='Toronto')
def test_change_email(self, mock_basket): # When a user's email is changed, their old email is unsubscribed # from all newsletters and their new email is subscribed to them. # Create a new user email = '*****@*****.**' token = 'first token' mock_basket.lookup_user.return_value = { 'email': email, # the old value 'token': token, 'newsletters': ['foo', 'bar'] } mock_basket.subscribe.return_value = { 'token': token, } country = CountryFactory.create(name='Greece', code='gr') city = CityFactory.create(name='Athens', country=country) user = UserFactory.create(email=email, userprofile={ 'geo_country': country, 'geo_city': city }) up = UserProfile.objects.get(pk=user.userprofile.pk) eq_(token, up.basket_token) new_email = '*****@*****.**' new_token = 'NEW token' mock_basket.subscribe.return_value = { 'token': new_token, } user.email = new_email user.save() mock_basket.lookup_user.assert_called_with(token=token) mock_basket.unsubscribe.assert_called_with( token=token, email=email, optout='Y', newsletters=[settings.BASKET_NEWSLETTER]) mock_basket.subscribe.assert_called_with(new_email, [settings.BASKET_NEWSLETTER], trigger_welcome='N', sync='Y') up = UserProfile.objects.get(pk=user.userprofile.pk) eq_(new_token, up.basket_token)
def test_extract_document(self): country = CountryFactory.create(name="Greece", code="gr") region = RegionFactory.create(name="attika", country=country) city = CityFactory.create(name="athens", region=region, country=country) user = UserFactory.create( userprofile={ "geo_city": city, "geo_region": region, "allows_community_sites": False, "allows_mozilla_sites": False, "geo_country": country, "full_name": "Nikos Koukos", "bio": "This is my bio", } ) profile = user.userprofile group_1 = GroupFactory.create() group_2 = GroupFactory.create() skill_1 = SkillFactory.create() skill_2 = SkillFactory.create() LanguageFactory.create(code="fr", userprofile=profile) LanguageFactory.create(code="en", userprofile=profile) group_1.add_member(profile) group_2.add_member(profile) profile.skills.add(skill_1) profile.skills.add(skill_2) result = UserProfile.extract_document(user.userprofile.id) ok_(isinstance(result, dict)) eq_(result["id"], profile.id) eq_(result["is_vouched"], profile.is_vouched) eq_(result["region"], region.name) eq_(result["city"], city.name) eq_(result["allows_community_sites"], profile.allows_community_sites) eq_(result["allows_mozilla_sites"], profile.allows_mozilla_sites) eq_(result["country"], country.name) eq_(result["fullname"], profile.full_name.lower()) eq_(result["name"], profile.full_name.lower()) eq_(result["bio"], profile.bio) eq_(result["has_photo"], False) eq_(result["groups"], [group_1.name, group_2.name]) eq_(result["skills"], [skill_1.name, skill_2.name]) eq_(set(result["languages"]), set([u"en", u"fr", u"english", u"french", u"français"]))
def setUp(self): self.user = UserFactory.create( email="*****@*****.**", userprofile={"geo_country": None, "geo_region": None, "geo_city": None} ) self.data = { "full_name": self.user.userprofile.full_name, "email": self.user.email, "username": self.user.username, "lat": 40.005814, "lng": -3.42071, "externalaccount_set-MAX_NUM_FORMS": "1000", "externalaccount_set-INITIAL_FORMS": "0", "externalaccount_set-TOTAL_FORMS": "0", "language_set-MAX_NUM_FORMS": "1000", "language_set-INITIAL_FORMS": "0", "language_set-TOTAL_FORMS": "0", } self.country = CountryFactory.create(mapbox_id="country1", name="Petoria") self.region = RegionFactory.create(country=self.country, mapbox_id="reg1", name="Ontario") self.city = CityFactory.create(region=self.region, mapbox_id="city1", name="Toronto")
def test_change_email(self, mock_basket): # When a user's email is changed, their old email is unsubscribed # from all newsletters and their new email is subscribed to them. # Create a new user email = '*****@*****.**' token = 'first token' mock_basket.lookup_user.return_value = { 'email': email, # the old value 'token': token, 'newsletters': ['foo', 'bar'] } mock_basket.subscribe.return_value = { 'token': token, } country = CountryFactory.create(name='Greece', code='gr') city = CityFactory.create(name='Athens', country=country) user = UserFactory.create(email=email, userprofile={'geo_country': country, 'geo_city': city}) up = UserProfile.objects.get(pk=user.userprofile.pk) eq_(token, up.basket_token) new_email = '*****@*****.**' new_token = 'NEW token' mock_basket.subscribe.return_value = { 'token': new_token, } user.email = new_email user.save() mock_basket.lookup_user.assert_called_with(token=token) mock_basket.unsubscribe.assert_called_with( token=token, email=email, optout='Y', newsletters=[settings.BASKET_NEWSLETTER] ) mock_basket.subscribe.assert_called_with( new_email, [settings.BASKET_NEWSLETTER], trigger_welcome='N', sync='Y' ) up = UserProfile.objects.get(pk=user.userprofile.pk) eq_(new_token, up.basket_token)
def test_extract_document(self): country = CountryFactory.create(name='Greece', code='gr') region = RegionFactory.create(name='attika', country=country) city = CityFactory.create(name='athens', region=region, country=country) user = UserFactory.create(userprofile={'geo_city': city, 'geo_region': region, 'allows_community_sites': False, 'allows_mozilla_sites': False, 'geo_country': country, 'full_name': 'Nikos Koukos', 'bio': 'This is my bio'}) profile = user.userprofile group_1 = GroupFactory.create() group_2 = GroupFactory.create() skill_1 = SkillFactory.create() skill_2 = SkillFactory.create() LanguageFactory.create(code='fr', userprofile=profile) LanguageFactory.create(code='en', userprofile=profile) group_1.add_member(profile) group_2.add_member(profile) profile.skills.add(skill_1) profile.skills.add(skill_2) result = UserProfile.extract_document(user.userprofile.id) ok_(isinstance(result, dict)) eq_(result['id'], profile.id) eq_(result['is_vouched'], profile.is_vouched) eq_(result['region'], region.name) eq_(result['city'], city.name) eq_(result['allows_community_sites'], profile.allows_community_sites) eq_(result['allows_mozilla_sites'], profile.allows_mozilla_sites) eq_(set(result['country']), set(['gr', 'Greece'])) eq_(result['fullname'], profile.full_name.lower()) eq_(result['name'], profile.full_name.lower()) eq_(result['bio'], profile.bio) eq_(result['has_photo'], False) eq_(result['groups'], [group_1.name, group_2.name]) eq_(result['skills'], [skill_1.name, skill_2.name]) eq_(set(result['languages']), set([u'en', u'fr', u'english', u'french', u'français']))
def test_update_basket_task(self, mock_basket): # When a user is created or added to a group, the appropriate # calls to update basket are made email = '*****@*****.**' token = 'footoken' mock_basket.lookup_user.return_value = { 'email': email, } mock_basket.subscribe.return_value = { 'token': token, } country = CountryFactory.create(name='Greece', code='gr') city = CityFactory.create(name='Athens', country=country) user = UserFactory.create(email=email, userprofile={ 'geo_country': country, 'geo_city': city }) mock_basket.subscribe.reset_mock() # forget that subscribe was called group = GroupFactory.create(name='Web Development', functional_area=True) GroupFactory.create(name='Marketing', functional_area=True) data = { 'country': 'gr', 'city': 'Athens', 'WEB_DEVELOPMENT': 'Y', 'MARKETING': 'N' } group.add_member(user.userprofile) # We just added a group, we should not need to subscribe anything ok_(not mock_basket.subscribe.called) # But we do need to update their phonebook record mock_basket.request.assert_called_with('post', 'custom_update_phonebook', token=token, data=data)