Exemple #1
0
def signup(request):
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)

        if user_form.is_valid():
            user = user_form.save()

            user.set_password(user.password)
            user.save()

            profile = UserProfile()
            profile.user = user

            profile.save()

            # create an empty wishlist for the new user
            wishlist = Wishlist(user=profile)
            wishlist.save()

            # new user is good, log them in
            # note: use the django.contrib.auth backend rather than the social auth backend
            # when users sign up using this route
            login(request, user, backend=settings.AUTHENTICATION_BACKENDS[1])
            return redirect('myaccount')
        else:
            # bad sign up data, render the form and the errors
            # only pass signup form to template, assume user doesn't want to login
            context = {"error": user_form.errors, 'user_form': UserForm()}
            return render(request, 'ginthusiasm/login.html', context)
    else:
        # if not a post request, redirect to login page
        return redirect('login')
Exemple #2
0
def add_user(username, first_name, last_name, email, password):
    user = User.objects.get_or_create(username=username)[0]
    user.first_name = first_name
    user.last_name = last_name
    user.email = email
    user.password = make_password(password)

    user.save()

    profile = UserProfile(user=user)
    profile.save()

    return user
Exemple #3
0
    def setUp(self):
        # Create a test user
        self.user = User.objects.create(username="******")
        self.user.first_name = "John"
        self.user.last_name = "Smith"
        self.user.email = "*****@*****.**"
        self.user.password = make_password("jsmith123")

        self.user.save()

        # Create a test user profile
        self.profile = UserProfile(user=self.user, user_type=UserProfile.BASIC)
        self.profile.save()

        self.client = Client()
Exemple #4
0
def create_user(strategy, details, response, user=None, *args, **kwargs):
    if user:
        user = User.objects.get(username=details['username'])
        user_profile = getattr(user, 'userprofile', None)

        if user_profile is None:
            user.first_name = details['first_name']
            user.last_name = details['last_name']
            user.email = details['email']
            user.password = ''
            user.user_type = UserProfile.BASIC
            user.is_staff = False
            user.is_superuser = False
            user.save()

            profile = UserProfile(
                user=user,
                user_type=UserProfile.BASIC,
            )
            profile.save()

            wishlist = Wishlist(user=profile)
            wishlist.save()
Exemple #5
0
    def setUp(self):
        # Set up a test user
        self.user = User.objects.create(username="******")
        self.user.first_name = "Test"
        self.user.last_name = "User"
        self.user.email = "*****@*****.**"
        self.user.password = make_password("testuser123")

        # Create a test user profile
        self.user.userprofile = UserProfile(user=self.user, user_type=UserProfile.EXPERT)
        self.user.userprofile.save()
        self.user.save()

        #Create an article

        user = User.objects.get(username='******')
        userprofile = user.userprofile
        add_article("Article1", "Short Description 1", "Content 1", 2016-12-02, "article1", userprofile, "articles/harris.jpg", False)

        self.client = Client()
Exemple #6
0
class UserTestCase(TestCase):
    def setUp(self):
        # Create a test user
        self.user = User.objects.create(username="******")
        self.user.first_name = "John"
        self.user.last_name = "Smith"
        self.user.email = "*****@*****.**"
        self.user.password = make_password("jsmith123")

        self.user.save()

        # Create a test user profile
        self.profile = UserProfile(user=self.user, user_type=UserProfile.BASIC)
        self.profile.save()

        self.client = Client()

        self.gins = [
            Gin.objects.create(name="TestGin1"),
            Gin.objects.create(name="TestGin2"),
            Gin.objects.create(name="TestGin3"),
        ]

        Wishlist.objects.create(user=self.profile)

    # Check the wishlist has been created
    def test_wishlist_exists(self):
        self.assertIsNotNone(self.user.userprofile.wishlist, "Wishlist exists")

    # Check the wishlist page exists and has 0 gins on it
    def test_wishlist_page_exists(self):
        response = self.client.get(
            reverse('wishlist', kwargs={'username': '******'}))
        self.assertContains(
            response, "This user hasn't added any gins to their wishlist!")
        self.assertTemplateUsed(response,
                                'ginthusiasm/wishlist.html',
                                msg_prefix="Wishlist uses wishlist template")

    # Check gins can be added to the wishlist
    def test_wishlist_add_remove(self):
        wishlist = self.user.userprofile.wishlist

        self.assertEqual(len(wishlist.gins.all()), 0, "Wishlist empty")

        for gin in self.gins:
            wishlist.gins.add(gin)

        self.assertEqual(len(wishlist.gins.all()), 3)

        for i in range(0, 2):
            self.assertTrue(self.gins[i] in wishlist.gins.all())

        # Check the template contains the names of all the gins
        response = self.client.get(
            reverse('wishlist', kwargs={'username': '******'}))
        self.assertContains(response, "TestGin1")
        self.assertContains(response, "TestGin2")
        self.assertContains(response, "TestGin3")
        self.assertTemplateUsed(template_name='ginthusiasm/gin_widget.html',
                                msg_prefix="Wishlist gin_widget used")

        wishlist.gins.remove(self.gins[0])
        self.assertEqual(len(wishlist.gins.all()), 2)

        # Check the template no longer contains TestGin1
        response = self.client.get(
            reverse('wishlist', kwargs={'username': '******'}))
        self.assertNotContains(response, "TestGin1")
        self.assertContains(response, "TestGin2")
        self.assertContains(response, "TestGin3")
        self.assertTemplateUsed(template_name='ginthusiasm/gin_widget.html',
                                msg_prefix="Wishlist gin_widget used")

        wishlist.gins.clear()
        self.assertEqual(len(wishlist.gins.all()), 0)

    def test_wishlist_post_addremove(self):
        self.client.login(username="******", password="******")
        user = auth.get_user(self.client)

        data = {
            'gin_slug': self.gins[0].slug,
            'user': user,
        }

        # check wishlist is empty
        user_gins = user.userprofile.wishlist.gins.all()
        self.assertEqual(len(user_gins), 0, "Wishlist empty before post")

        # add a gin by posting to view
        self.client.post(reverse("wishlist_add"), data)

        # check gin just added is now in the wishlist
        user_gins = user.userprofile.wishlist.gins.all()
        self.assertTrue(self.gins[0] in user_gins, "Wishlist add by post")
        self.assertEqual(len(user_gins), 1, "Wishlist post has 1")

        # remove same the gin by post
        self.client.post(reverse("wishlist_add"), data)

        # check the gin has been removed
        user_gins = user.userprofile.wishlist.gins.all()
        self.assertTrue(self.gins[0] not in user_gins,
                        "Wishlist remove by post (not in)")
        self.assertEqual(len(user_gins), 0, "Wishlist remove by post (len)")
def populate_users():
    print("Populating users...")

    users = [{
        "username": "******",
        "first_name": "Catherine",
        "last_name": "Daly",
        "email": "*****@*****.**",
        "password": make_password("catherineadmin"),
        "user_type": UserProfile.ADMIN,
    }, {
        "username": "******",
        "first_name": "Robert",
        "last_name": "Gilmour",
        "email": "*****@*****.**",
        "password": make_password("robertadmin"),
        "user_type": UserProfile.ADMIN,
    }, {
        "username": "******",
        "first_name": "Matthew",
        "last_name": "Smith",
        "email": "*****@*****.**",
        "password": make_password("mattadmin"),
        "user_type": UserProfile.ADMIN,
    }, {
        "username": "******",
        "first_name": "Rosalyn",
        "last_name": "Taylor",
        "email": "*****@*****.**",
        "password": make_password("rozzadmin"),
        "user_type": UserProfile.ADMIN,
    }, {
        "username": "******",
        "first_name": "Alice",
        "last_name": "Alison",
        "email": "*****@*****.**",
        "password": make_password("alicetest"),
        "user_type": UserProfile.EXPERT,
    }, {
        "username": "******",
        "first_name": "Bob",
        "last_name": "Bobertson",
        "email": "*****@*****.**",
        "password": make_password("bobtest"),
        "user_type": UserProfile.BASIC,
    }, {
        "username": "******",
        "first_name": "Charles",
        "last_name": "Charleston",
        "email": "*****@*****.**",
        "password": make_password("charlietest"),
        "user_type": UserProfile.DISTILLERY_OWNER,
    }, {
        "username": "******",
        "first_name": "Sharon",
        "last_name": "Jones",
        "email": "*****@*****.**",
        "password": make_password("sharontest"),
        "user_type": UserProfile.BASIC,
    }, {
        "username": "******",
        "first_name": "Hector",
        "last_name": "Ali",
        "email": "*****@*****.**",
        "password": make_password("sharontest"),
        "user_type": UserProfile.EXPERT,
    }]

    for data in users:
        user, created = User.objects.get_or_create(username=data['username'])

        if created:
            user.first_name = data['first_name']
            user.last_name = data['last_name']
            user.email = data['email']
            user.password = data['password']
            user.is_staff = data['user_type'] == UserProfile.ADMIN
            user.is_superuser = data['user_type'] == UserProfile.ADMIN
            user.save()

            profile = UserProfile(
                user=User.objects.get(username=data['username']),
                user_type=data['user_type'],
                profile_image="profile_images/" + data['username'].lower() +
                "-profile.jpg")
            profile.save()
Exemple #8
0
class UserTestCase(TestCase):
    def setUp(self):
        # Create a test user
        self.user = User.objects.create(username="******")
        self.user.first_name = "John"
        self.user.last_name = "Smith"
        self.user.email = "*****@*****.**"
        self.user.password = make_password("jsmith123")

        self.user.save()

        # Create a test user profile
        self.profile = UserProfile(user=self.user, user_type=UserProfile.BASIC)
        self.profile.save()

        self.client = Client()

    # Checks the user has been succcessfully added to the database
    def test_user_exists(self):
        self.assertIsNotNone(self.user, "User not null")
        profile = self.user.userprofile
        self.assertIsNotNone(profile, "Profile not null")

    # Checks the user's data has been saved correctly
    def test_user_data(self):
        self.assertEqual(self.user.username, "jsmith", "Username match")
        self.assertEqual(self.user.first_name, "John", "User first_name match")
        self.assertEqual(self.user.last_name, "Smith", "User last_name match")
        self.assertEqual(self.user.email, "*****@*****.**", "User email match")

    # Check that the user is able to log in
    def test_user_login(self):
        self.client.login(username="******", password="******")

        user = auth.get_user(self.client)
        self.assertTrue(user.is_authenticated(), "User is authenticated")

        # Check the page has a log out and my account button
        response = self.client.get("/")
        self.assertContains(response,
                            "My Account",
                            msg_prefix="Logged in user has my account button")
        self.assertContains(response,
                            "Log Out",
                            msg_prefix="Logged in user has log out button")

    # Check that the user is redirected to their 'My Account' page after logging in
    def test_user_redirect_after_login(self):
        response = self.client.post(reverse("login"), {
            "username": "******",
            "password": "******"
        })
        self.assertRedirects(response,
                             '/my-account/',
                             msg_prefix="Redirect after user login")

    # Check that the user can be logged out
    def test_user_logout(self):
        self.client.force_login(self.user)

        user = auth.get_user(self.client)
        self.assertTrue(user.is_authenticated(), "User is authenticated")
        self.assertFalse(user.is_anonymous(), "User not anonymous after login")

        self.client.logout()
        user = auth.get_user(self.client)
        self.assertFalse(user.is_authenticated(),
                         "User not authenticated after log out")
        self.assertTrue(user.is_anonymous(), "User is anonymous after log out")

        # Check the index page has the login button after logging out
        response = self.client.get('/')
        self.assertContains(
            response,
            'Log In | Sign Up',
            msg_prefix="Index page has login button after logout")

    # Create a new user by posting to the signup url
    def test_signup(self):
        data = {
            "username": "******",
            "password": "******",
            "first_name": "test",
            "last_name": "user",
            "email": "*****@*****.**",
        }

        self.client.post(reverse('signup'), data)

        newUser = User.objects.get(username=data["username"])
        self.assertIsNotNone(newUser, "New user signed up")

        # log the new user in and out
        self.client.login(username=data["username"], password=data["password"])
        self.assertTrue(newUser.is_authenticated(), "New user can log in")

        self.client.logout()
        newUser = auth.get_user(self.client)
        self.assertFalse(newUser.is_authenticated(), "New user can log out")