コード例 #1
0
    def test_set_up(self):
        self.assertEqual(User.objects.all().count(), 1,
                         "After setup, must contain one user!")
        self.assertEqual(Section.objects.all().count(), 0)
        self.assertEqual(Course.objects.all().count(), 0)

        self.assertEqual(User.objects.filter(username="******").count(), 1)
        u = User.objects.get(username="******")
        self.assertIsNotNone(u)
        self.assertEqual(u.password, "123")
        self.assertEqual(u.role, dict(User.ROLES)["S"])

        # Testing database flushing (Overwrite = True
        newuser = User(username="******", password="******", role="")
        newuser.save()
        newcourse = Course(dept="CS", cnum="351")
        newcourse.save()
        newsection = Section(snum="801", course=newcourse)
        newsection.save()

        u = User.objects.get(username="******")
        c = Course.objects.get(dept="CS", cnum="351")
        s = Section.objects.get(snum="801",
                                course__dept="CS",
                                course__cnum="351")
        self.assertIsNotNone(u)
        self.assertIsNotNone(c)
        self.assertIsNotNone(s)

        # Should rebuild database, with none of the following models we had created:
        DjangoStorageManager.set_up(overwrite=True)
        self.assertEqual(User.objects.all().count(), 1,
                         "After rebuild (overwrite), must contain one user!")
        self.assertEqual(Section.objects.all().count(), 0)
        self.assertEqual(Course.objects.all().count(), 0)
コード例 #2
0
 def set_up(overwrite=False) -> bool:
     retVal = False
     if len(Section.objects.all()) > 0 or len(
             User.objects.all()) > 0 or len(Course.objects.all()) > 0:
         # Database isn't empty!
         if not overwrite:
             retVal = False
         else:
             for section in Section.objects.all():
                 section.delete()
             for user in User.objects.all():
                 user.delete()
             for course in Course.objects.all():
                 course.delete()
             retVal = True
     sup = User(username="******",
                password="******",
                role=dict(User.ROLES)["S"])
     DjangoStorageManager.insert_user(sup)
     group = Group.objects.get(name=sup.role)
     if group:
         group.user_set.add(sup)
         sup.is_superuser = True
         sup.set_password(sup.password)
     DjangoStorageManager.insert_user(sup)
     return retVal
コード例 #3
0
 def test_get_user(self):
     u = User(username="******", password="******", role=dict(User.ROLES)["I"])
     u.save()
     retval = DjangoStorageManager.get_user("Rock")
     self.assertIsNotNone(retval)
     self.assertIsInstance(retval, User)
     self.assertEqual(retval.password, "123")
     self.assertEqual(retval.role, dict(User.ROLES)["I"])
コード例 #4
0
ファイル: test_views.py プロジェクト: carpiotyler/TA-App-Copy
 def setUp(self):
     self.UserDict = {
         "default": Staff(),
         "ta": Staff(),
         "ins": Staff(),
         "admin": Staff(),
         "sup": Staff()
     }
     self.UserDict['default'].groups.add(DefaultGroup)
     self.UserDict['ta'].groups.add(TAGroup)
     self.UserDict['ins'].groups.add(InsGroup)
     self.UserDict['admin'].groups.add(AdminGroup)
     self.UserDict['sup'].groups.add(SupGroup)
コード例 #5
0
    def insert_user(user: User) -> bool:
        existinguser = DjangoStorageManager.get_user(user.username)
        overwritten = False

        # Checking if user already exists
        if existinguser != None:
            # overwrite case, just setting new users id to the old one we found, then overwriting
            user.id = existinguser.id
            user.save()
            overwritten = True
        else:
            #new user, just save it
            user.save()
            overwritten = False

        return overwritten
コード例 #6
0
 def test_insert_user(self):
     u = User(username="******", password="******")
     self.assertFalse(DjangoStorageManager.insert_user(u),
                      "Should return false, not overwriting!")
     retval = User.objects.get(username="******")
     self.assertIsNotNone(retval)
     u.password = "******"
     u.role = dict(User.ROLES)["I"]
     self.assertTrue(DjangoStorageManager.insert_user(u),
                     "Should return true, overwriting!")
     retval = User.objects.get(username="******")
     self.assertIsNotNone(retval)
     self.assertEqual(User.objects.all().count(), 2,
                      "Should only be 2 users in Users during this test!")
     self.assertEqual(retval.password, "password",
                      "Insert didn't properly update the db!")
     self.assertEqual(retval.role,
                      dict(User.ROLES)["I"], "Should have updated roles!")
コード例 #7
0
    def test_get_users_by(self):
        u1 = User(username="******", password="******", role=dict(User.ROLES)["I"])
        u2 = User(username="******",
                  password="******",
                  role=dict(User.ROLES)["I"])
        u1.save()
        u2.save()

        # Testing getting user by providing username(should be unique - 1 user!)
        retval = DjangoStorageManager.get_users_by(username="******")
        self.assertIsInstance(retval, list)
        self.assertEqual(len(retval), 1)
        self.assertTrue(retval.__contains__(u1))

        # Testing getting user by providing role
        retval = DjangoStorageManager.get_users_by(
            role="Instructor")  # dict(User.ROLES)["I"] -> Instructor
        self.assertIsInstance(retval, list)
        self.assertEqual(len(retval), 2)
        self.assertTrue(retval.__contains__(u1))
        self.assertTrue(retval.__contains__(u2))

        # Testing getting course by providing nothing (all users (sup included))
        retval = DjangoStorageManager.get_users_by()
        self.assertIsInstance(retval, list)
        self.assertEqual(len(retval), 3)
        self.assertTrue(retval.__contains__(u1))
        self.assertTrue(retval.__contains__(u2))
コード例 #8
0
    def test_delete(self):
        c = Course(dept="CS", cnum="351")
        s = Section(snum="801", course=c)
        u = User(username="******", password="******")

        # None of these objects are in the database, so nothing should be deleted
        self.assertFalse(DjangoStorageManager.delete(c))
        self.assertFalse(DjangoStorageManager.delete(s))
        self.assertFalse(DjangoStorageManager.delete(u))

        c.save()
        s.save()
        u.save()
        self.assertEqual(Course.objects.all().count(), 1)
        self.assertEqual(Section.objects.all().count(), 1)
        self.assertEqual(User.objects.all().count(), 2)

        # Deleting objects here
        self.assertTrue(DjangoStorageManager.delete(s))
        self.assertTrue(DjangoStorageManager.delete(c))
        self.assertTrue(DjangoStorageManager.delete(u))
        self.assertEqual(Course.objects.all().count(), 0)
        self.assertEqual(Section.objects.all().count(), 0)
        self.assertEqual(User.objects.all().count(), 1)

        c = Course(dept="CS", cnum="351")
        s = Section(snum="801", course=c)
        u = User(username="******", password="******")

        c.save()
        s.save()
        u.save()

        self.assertEqual(Course.objects.all().count(), 1)
        self.assertEqual(Section.objects.all().count(), 1)
        self.assertEqual(User.objects.all().count(), 2)
コード例 #9
0
    def add(self, fields: dict):
        # Need username and password to add a user!
        if 'username' not in fields.keys():
            return False, "Please fill out username"

        user = self.storage.get_user(fields["username"])
        if user is None:  # user dne!
            user = User()
            user.username = fields["username"]

            # if fields['role'] is either None or "", set fields['role'] to 'Default'
            # if fields['role'] exists and is invalid, return false. Otherwise, set role as fields['role']
            if 'role' not in fields.keys(
            ) or fields['role'] is None or fields['role'].strip() == "":
                user.role = dict(User.ROLES)['D']
            elif fields['role'] in dict(User.ROLES).values():
                user.role = fields['role']
            else:
                return False, "Role invalid!"

            # Unvalidated fields...
            if 'password' in fields.keys(
            ) and fields['password'] is not None and len(
                    fields['password'].strip()) > 0:
                user.set_password(fields['password'])
            if 'phone_number' in fields.keys(
            ) and fields['phone_number'] is not None and len(
                    fields['phone_number'].strip()) > 0:
                user.phonenum = fields['phone_number']
            if 'address' in fields.keys(
            ) and fields['address'] is not None and len(
                    fields['address'].strip()) > 0:
                user.address = fields['address']
            if 'firstname' in fields.keys(
            ) and fields['firstname'] is not None and len(
                    fields['firstname'].strip()) > 0:
                user.firstname = fields['firstname']
            if 'lastname' in fields.keys(
            ) and fields['lastname'] is not None and len(
                    fields['lastname'].strip()) > 0:
                user.lastname = fields['lastname']
            if 'email' in fields.keys(
            ) and fields['email'] is not None and len(
                    fields['email'].strip()) > 0:
                user.email = fields['email']
            if 'bio' in fields.keys() and fields['bio'] is not None and len(
                    fields['bio'].strip()) > 0:
                user.bio = fields['bio']

            self.storage.insert_user(user)

            #Add user to group
            group = Group.objects.get(name=fields['role'].strip())
            if group:
                group.user_set.add(user)

                #Just for testing
                print("User Permissions: " + str(user.get_all_permissions()))
            else:
                self.storage.delete(user)
                return False, "Unable to assign user to group."
            return True, ""

        else:  # user exists
            return False, "User Exists!"