Beispiel #1
0
 def setUp(self):
     self.user1 = User.objects.create_user("a", "*****@*****.**", "a")
     self.famille = models.Famille(user=self.user1, email="*****@*****.**")
     self.famille.save()
     self.user2 = User.objects.create_user("b", "*****@*****.**", "b")
     self.presta = models.Prestataire(user=self.user2, description="Une description", email="*****@*****.**")
     self.presta.save()
     self.user3 = User.objects.create_user("c", "*****@*****.**", "c")
     self.famille_fav = FamilleFavorite(
         object_type="Prestataire", object_id=self.presta.pk, famille=self.famille
     )
     self.famille_fav.save()
     self.prestataire_fav = PrestataireFavorite(
         object_type="Famille", object_id=self.famille.pk, prestataire=self.presta
     )
     self.prestataire_fav.save()
     self.keygroup = KeyGroup(name='activate_account', generator="sha1-hex")
     self.keygroup.save()
Beispiel #2
0
class ModelsTestCase(TestCase):

    def setUp(self):
        self.user1 = User.objects.create_user("a", "*****@*****.**", "a")
        self.famille = models.Famille(user=self.user1, email="*****@*****.**")
        self.famille.save()
        self.user2 = User.objects.create_user("b", "*****@*****.**", "b")
        self.presta = models.Prestataire(user=self.user2, description="Une description", email="*****@*****.**")
        self.presta.save()
        self.user3 = User.objects.create_user("c", "*****@*****.**", "c")
        self.famille_fav = FamilleFavorite(
            object_type="Prestataire", object_id=self.presta.pk, famille=self.famille
        )
        self.famille_fav.save()
        self.prestataire_fav = PrestataireFavorite(
            object_type="Famille", object_id=self.famille.pk, prestataire=self.presta
        )
        self.prestataire_fav.save()
        self.keygroup = KeyGroup(name='activate_account', generator="sha1-hex")
        self.keygroup.save()

    def tearDown(self):
        User.objects.all().delete()
        models.Famille.objects.all().delete()
        models.Prestataire.objects.all().delete()
        models.Geolocation.objects.all().delete()
        FamilleFavorite.objects.all().delete()
        PrestataireFavorite.objects.all().delete()
        self.keygroup.delete()

    def test_simple_id(self):
        self.assertEqual(self.famille.simple_id, "famille__%s" % self.famille.pk)
        self.assertEqual(self.presta.simple_id, "prestataire__%s" % self.presta.pk)

    def test_get_user_related(self):
        self.assertIsInstance(models.get_user_related(self.user1), models.Famille)
        self.assertIsInstance(models.get_user_related(self.user2), models.Prestataire)
        self.assertRaises(ObjectDoesNotExist, models.get_user_related, self.user3)

    def test_has_user_related(self):
        self.assertTrue(models.has_user_related(self.user1))
        self.assertTrue(models.has_user_related(self.user2))
        self.assertFalse(models.has_user_related(self.user3))

        anonymous = AnonymousUser()
        self.assertFalse(models.has_user_related(anonymous))

    def test_is_geolocated(self):
        geoloc = models.Geolocation(lat=33.01, lon=2.89)
        geoloc.save()

        self.assertFalse(self.famille.is_geolocated)

        self.famille.geolocation = geoloc
        self.famille.save()
        self.assertTrue(self.famille.is_geolocated)

        geoloc.has_error = True
        geoloc.save()
        self.assertFalse(self.famille.is_geolocated)

    @patch("famille.utils.geolocation.geolocate")
    def test_geolocate(self, geolocate):
        geolocate.return_value = 48.895603, 2.322858
        self.famille.street = "32 rue des Epinettes"
        self.famille.postal_code = "75017"
        self.famille.city = "Paris"
        self.famille.country = "France"
        self.famille.save()

        self.famille.geolocate()
        geolocate.assert_called_with("32 rue des Epinettes 75017 Paris, France")
        self.assertIsNotNone(models.Geolocation.objects.filter(lat=48.895603, lon=2.322858).first())
        self.assertEqual(self.famille.geolocation.lat, 48.895603)
        self.assertEqual(self.famille.geolocation.lon, 2.322858)

    @patch("famille.models.users.UserInfo.geolocate")
    def test_manage_geolocation(self, mock):
        self.famille.country = "France"  # not enough
        self.famille.save()
        self.famille.manage_geolocation(["country"])
        self.assertFalse(mock.called)

        self.famille.street = "32 rue des Epinettes"  # not enough
        self.famille.save()
        self.famille.manage_geolocation(["street"])
        self.assertFalse(mock.called)

        self.famille.city = "Paris"  # enough info to geolocate
        self.famille.save()
        self.famille.manage_geolocation(["city"])
        self.assertTrue(mock.called)
        mock.reset_mock()

        self.famille.city = ""
        self.famille.postal_code = "75017"  # enough info to geolocate
        self.famille.save()
        self.famille.manage_geolocation(["city"])
        self.assertTrue(mock.called)

    def test_add_favorite(self):
        uri = "/api/v1/prestataires/%s" % self.presta.pk
        self.famille.add_favorite(uri)
        self.assertEqual(self.famille.favorites.all().count(), 1)
        qs = FamilleFavorite.objects.filter(
            famille=self.famille, object_id=self.presta.pk, object_type="Prestataire"
        )
        self.assertEqual(qs.count(), 1)

        # cannot add same favorite
        self.famille.add_favorite(uri)
        self.assertEqual(self.famille.favorites.all().count(), 1)
        qs = FamilleFavorite.objects.filter(
            famille=self.famille, object_id=self.presta.pk, object_type="Prestataire"
        )
        self.assertEqual(qs.count(), 1)

    def test_remove_favorite(self):
        uri = "/api/v1/prestataires/%s" % self.presta.pk
        FamilleFavorite(famille=self.famille, object_id=self.presta.pk, object_type="Prestataire").save()
        self.famille.remove_favorite(uri)

        self.assertEqual(self.famille.favorites.all().count(), 0)

    def test_get_favorites_data_presta(self):
        favs = self.famille.get_favorites_data()
        self.assertIsInstance(favs, collections.OrderedDict)
        self.assertIn("Prestataire", favs)
        self.assertEqual(len(favs["Prestataire"]), 1)
        self.assertIsInstance(favs["Prestataire"][0], models.Prestataire)
        self.assertEqual(favs["Prestataire"][0].description, self.presta.description)

    def test_get_favorites_data_famille(self):
        favs = self.presta.get_favorites_data()
        self.assertIsInstance(favs, collections.OrderedDict)
        self.assertIn("Famille", favs)
        self.assertEqual(len(favs["Famille"]), 1)
        self.assertIsInstance(favs["Famille"][0], models.Famille)
        self.assertEqual(favs["Famille"][0].description, self.famille.description)

    def test_get_resource_uri(self):
        out = "/api/v1/familles/%s" % self.famille.pk
        self.assertEqual(self.famille.get_resource_uri(), out)

        out = "/api/v1/prestataires/%s" % self.presta.pk
        self.assertEqual(self.presta.get_resource_uri(), out)

    def test_get_user(self):
        self.assertEqual(self.famille_fav.get_user(), self.presta)
        self.assertEqual(self.prestataire_fav.get_user(), self.famille)

    def test_create_user(self):
        user = models.UserInfo.create_user(self.user3, "prestataire")
        self.assertIsInstance(user, models.Prestataire)
        self.assertEqual(user.email, self.user3.email)

        user.delete()
        user = models.UserInfo.create_user(self.user3, "famille")
        self.assertIsInstance(user, models.Famille)
        self.assertEqual(user.email, self.user3.email)

    def test_create_user_free_plan(self):
        user = models.UserInfo.create_user(self.user3, "famille")
        self.assertTrue(user.is_premium)
        self.assertEqual(user.plan_expires_at, models.Famille.FREE_PLAN_EXPIRATION)

        models.Famille.FREE_PLAN_LIMIT = datetime(2013, 7, 1, tzinfo=utc)
        user = models.UserInfo.create_user(self.user2, "famille")
        self.assertFalse(user.is_premium)

    def test_profile_access_is_authorized(self):
        request = MagicMock()
        self.assertTrue(self.presta.profile_access_is_authorized(request))
        # no global
        self.famille.visibility_global = False
        self.assertFalse(self.famille.profile_access_is_authorized(request))

        self.famille.visibility_global = True
        request.user = AnonymousUser()
        self.assertFalse(self.famille.profile_access_is_authorized(request))

        self.famille.visibility_prestataire = False
        request.user = self.user2
        self.assertFalse(self.famille.profile_access_is_authorized(request))
        self.famille.visibility_prestataire = True
        self.assertTrue(self.famille.profile_access_is_authorized(request))

        self.famille.visibility_family = False
        request.user = self.famille.user
        self.assertTrue(self.famille.profile_access_is_authorized(request))
        request.user = User.objects.create_user("d", "*****@*****.**", "d")
        models.Famille(user=request.user, email="*****@*****.**").save()
        self.assertFalse(self.famille.profile_access_is_authorized(request))
        self.famille.visibility_family = True
        self.assertTrue(self.famille.profile_access_is_authorized(request))

    def test_user_is_located(self):
        user = AnonymousUser()
        self.assertFalse(models.user_is_located(user))

        user = self.user1
        self.assertFalse(models.user_is_located(user))

        geoloc = models.Geolocation(lat=12.2, lon=12.2)
        geoloc.save()
        self.famille.geolocation = geoloc
        self.famille.save()
        self.assertTrue(models.user_is_located(user))

        geoloc.has_error = True
        geoloc.save()
        self.assertFalse(models.user_is_located(user))

    def test_get_pseudo(self):
        self.assertEqual(self.presta.get_pseudo(), "b")
        self.presta.first_name = "Joe"
        self.assertEqual(self.presta.get_pseudo(), "Joe")
        self.presta.name = "Jack"
        self.assertEqual(self.presta.get_pseudo(), "Joe J.")

        self.assertEqual(self.famille.get_pseudo(), "a")
        self.famille.first_name = "Mick"
        self.assertEqual(self.famille.get_pseudo(), "Mick")
        self.famille.name = "Down"
        self.assertEqual(self.famille.get_pseudo(), "Mick D.")

    def test_compute_user_visibility_filters(self):
        user = AnonymousUser()
        f = models.compute_user_visibility_filters(user)
        self.assertEqual(f.children, [('visibility_global', True)])

        f = models.compute_user_visibility_filters(self.user1)
        self.assertEqual(f.children, [('visibility_global', True), ('visibility_family', True)])

        f = models.compute_user_visibility_filters(self.user2)
        self.assertEqual(f.children, [('visibility_global', True), ('visibility_prestataire', True)])

    @patch("django.core.mail.message.EmailMessage.send")
    def test_send_verification_email(self, send):
        req = HttpRequest()
        req.META = {"HTTP_HOST": "toto.com"}
        self.presta.send_verification_email(req)
        self.assertTrue(send.called)
        self.assertEqual(Key.objects.filter(claimed_by=self.presta.user, claimed=None).count(), 1)

    def test_verify_user(self):
        self.presta.verify_user(claimant=self.presta.user)
        presta = User.objects.get(pk=self.presta.user.pk)
        self.assertTrue(self.presta.user.is_active)

    def test_enfant_display_only_name(self):
        e = models.Enfant(e_name="John")
        self.assertEqual(e.display, "John")

    def test_enfant_display_no_birthday(self):
        e = models.Enfant(e_name="John", e_school="Best school in the World")
        self.assertEqual(e.display, u"John, scolarisé à Best school in the World")

    def test_enfant_display_no_school(self):
        today = datetime.now()
        bday = today - timedelta(days=360*10)
        e = models.Enfant(e_name="John", e_birthday=bday)
        self.assertEqual(e.display, "John, 9 ans")

    def test_enfant_display_all(self):
        today = datetime.now()
        bday = today - timedelta(days=360*10)
        e = models.Enfant(e_name="John", e_school="Best school in the World", e_birthday=bday)
        self.assertEqual(e.display, u"John, 9 ans, scolarisé à Best school in the World")

    def test_decode_users_ok(self):
        data = "---".join([self.famille.encoded, self.presta.encoded])
        expected = [self.famille, self.presta]
        self.assertEquals(UserInfo.decode_users(data), expected)

    def test_decode_users_notok(self):
        data = base64.urlsafe_b64encode(json.dumps({"type": "Prestataire", "pk": 118926}))
        self.assertRaises(ValueError, UserInfo.decode_users, data)

        data = "eaziouehazoenuazehazpoieybazioueh"
        self.assertRaises(ValueError, UserInfo.decode_users, data)

    @patch("django.core.mail.EmailMessage.send")
    def test_check_plan_expiration_basic(self, send):
        self.famille.plan = "basic"
        self.famille.save()
        check_plan_expiration(None, None, self.famille.user)
        f = models.Famille.objects.get(pk=self.famille.pk)
        self.assertEquals(f.plan, "basic")
        self.assertFalse(send.called)

    @patch("django.core.mail.EmailMessage.send")
    def test_check_plan_expiration_no_exp(self, send):
        self.famille.plan = "premium"
        self.famille.plan_expires_at = None
        self.famille.save()
        check_plan_expiration(None, None, self.famille.user)
        f = models.Famille.objects.get(pk=self.famille.pk)
        self.assertEquals(f.plan, "basic")
        self.assertIsNone(f.plan_expires_at)
        self.assertTrue(send.called)

    @patch("django.core.mail.EmailMessage.send")
    def test_check_plan_expiration_expired(self, send):
        self.famille.plan = "premium"
        self.famille.plan_expires_at = datetime(2000, 1, 1)
        self.famille.save()
        check_plan_expiration(None, None, self.famille.user)
        f = models.Famille.objects.get(pk=self.famille.pk)
        self.assertEquals(f.plan, "basic")
        self.assertIsNone(f.plan_expires_at)
        self.assertTrue(send.called)

    @patch("django.core.mail.EmailMessage.send")
    def test_check_plan_expiration_not_expired(self, send):
        self.famille.plan = "premium"
        self.famille.plan_expires_at = datetime(2500, 1, 1)
        self.famille.save()
        check_plan_expiration(None, None, self.famille.user)
        f = models.Famille.objects.get(pk=self.famille.pk)
        self.assertEquals(f.plan, "premium")
        self.assertEquals(f.plan_expires_at.replace(tzinfo=None), datetime(2500, 1, 1))
        self.assertFalse(send.called)

    def test_visibility_score_empty(self):
        p = models.Prestataire()
        self.assertEquals(p.visibility_score, 0)

    def test_visibility_score_not_empty(self):
        p = models.Prestataire(type="baby", name="To", first_name="Tou", street="Rue des Moines", city="Paris")
        self.assertEquals(p.visibility_score, 0.625)

    def test_visibility_score_is_enough_zero(self):
        p = models.Prestataire()
        p.MIN_VISIBILITY_SCORE = 0.9
        self.assertFalse(p.visibility_score_is_enough)

    def test_visibility_score_is_not_enough(self):
        p = models.Prestataire(type="baby", name="To", first_name="Tou", street="Rue des Moines", city="Paris")
        p.MIN_VISIBILITY_SCORE = 0.9
        self.assertFalse(p.visibility_score_is_enough)

    def test_visibility_score_is_enough(self):
        p = models.Prestataire(
            type="baby", name="To", first_name="Tou", street="Rue des Moines", city="Paris",
            postal_code="75017", birthday=datetime.now(), profile_pic="/dumbfile.txt"
        )
        p.MIN_VISIBILITY_SCORE = 0.9
        self.assertTrue(p.visibility_score_is_enough)