Ejemplo n.º 1
0
    def test_user_navbar(self, application: str,
                         ff_browser: webdriver.Firefox):
        ''' check that there is user profile button '''
        test_clean_DB()  # remove previous inserts in case there are any
        ff_browser.get(application + "/logout")  # logout

        connection = sqlite3.connect(DB_NAME)

        #insert normal user
        cur = connection.execute(
            "INSERT INTO Users VALUES (?,?,?,?,?,?,?,?,?,?)",
            (t_username, t_firstname, t_lastname, encryptPassword(t_password),
             1, 1, 1, 0, 0, ""))

        connection.commit()
        connection.close()

        #connect to user
        ff_browser.get(application + "/login")
        elem = ff_browser.find_element_by_name("username")
        elem.send_keys(t_username)
        elem = ff_browser.find_element_by_name("password")
        elem.send_keys(t_password)
        elem = ff_browser.find_element_by_name("submit")
        elem.click()
        elem = ff_browser.find_element_by_name("user_link")
        assert elem.text == "My Profile"
Ejemplo n.º 2
0
    def test_admin_navbar(self, application: str,
                          ff_browser: webdriver.Firefox):
        ''' check if Control Panel moves the admin to the right route '''
        test_clean_DB()  # remove previous inserts in case there are any
        ff_browser.get(application + "/logout")  # logout

        connection = sqlite3.connect(DB_NAME)

        #insert admin user
        cur = connection.execute(
            "INSERT INTO Users VALUES (?,?,?,?,?,?,?,?,?,?)",
            (t_username, t_firstname, t_lastname, encryptPassword(t_password),
             1, 1, 1, 1, 0, ""))

        connection.commit()
        connection.close()

        #connect to admin user
        ff_browser.get(application + "/login")
        elem = ff_browser.find_element_by_name("username")
        elem.send_keys(t_username)
        elem = ff_browser.find_element_by_name("password")
        elem.send_keys(t_password)
        elem = ff_browser.find_element_by_name("submit")
        elem.click()
        elem = ff_browser.find_element_by_name("control_panel_link")

        elem.click()
        assert ff_browser.current_url == application + "/controlpanel"
Ejemplo n.º 3
0
def updatePassword(userUpdate):
    # Connect to database and check if user exists
    con = sqlite3.connect(current_app.config['DB_NAME'])

    # Prepare the query
    sqlQueryUpdateUser = "******"

    # Run the query to get user data
    con.execute(sqlQueryUpdateUser,(encryptPassword(userUpdate.getPassword()),
                                     userUpdate.getUsername()))

    # Commit the changes
    con.commit()

    # Close the connection to the database
    con.close()
Ejemplo n.º 4
0
def db_prepare_manage_fac():
    global instID, facID

    # Prepare the institution
    db_name = "database.db"

    # connect to db to prepare it before testing
    con = sqlite3.connect(db_name)
    cursor = con.cursor()

    # Check if institution exists
    sqlQueryCheckExist = "SELECT * FROM Institutions WHERE InstitutionName = (?)"
    sqlRes = con.execute(sqlQueryCheckExist, (institution_test, ))
    record = sqlRes.fetchone()

    # If institution does not exists create it
    if record == None:
        sqtInsertInst = "INSERT INTO Institutions (InstitutionName) VALUES (?)"
        cursor.execute(sqtInsertInst, (institution_test, ))
        instID = cursor.lastrowid
    else:
        instID = record[0]

    # Check if faculty exists
    sqlQueryCheckExist = "SELECT * FROM Faculties WHERE FacultyName = (?)"
    sqlRes = con.execute(sqlQueryCheckExist, (faculty_test, ))
    record = sqlRes.fetchone()

    # If faculty does not exists create it
    if record == None:
        sqlInsertFac = "INSERT INTO Faculties (FacultyName) VALUES (?)"
        cursor.execute(sqlInsertFac, (faculty_test, ))
        facID = cursor.lastrowid
    else:
        facID = record[0]

    # Check if institution and faculty exists in FacIn table
    sqlQueryCheckExist = "SELECT * FROM FacIn WHERE InstitutionID = (?) AND FacultyID = (?)"
    sqlRes = con.execute(sqlQueryCheckExist, (instID, facID))
    record = sqlRes.fetchone()

    # If institution and faculty does not exists create it
    if record == None:
        sqtInsertInstFac = "INSERT INTO FacIn VALUES (?, ?)"
        con.execute(sqtInsertInstFac, (instID, facID))

    # Check if user exists in Users table
    sqlQueryCheckExist = "SELECT * FROM Users WHERE UserName = (?)"
    sqlRes = con.execute(sqlQueryCheckExist, (username_test, ))
    record = sqlRes.fetchone()

    # If user does not exists create it
    if record == None:
        sqtInsertUser = "******"
        con.execute(sqtInsertUser,
                    (username_test, "test1", "test1",
                     encryptPassword(password_test), instID, facID, 2, ""))

    # Commit the changes in users table
    con.commit()

    #----------------------------------------------------------------
    yield db_name

    # Check if user exists
    sqlQueryCheckExist = "SELECT * FROM Users WHERE UserName = (?)"
    sqlRes = con.execute(sqlQueryCheckExist, (username_test, ))
    record = sqlRes.fetchone()

    # If user exists delete the user from DB
    if record != None:
        sqlDelete = "DELETE FROM Users WHERE UserName = (?)"
        sqlRes = con.execute(sqlDelete, (username_test, ))

    # Check if institution and faculty exists in FacIn table
    sqlQueryCheckExist = "SELECT * FROM FacIn WHERE InstitutionID = (?) AND FacultyID = (?)"
    sqlRes = con.execute(sqlQueryCheckExist, (instID, facID))
    record = sqlRes.fetchone()

    # If faculty in institution exists delete it
    if record != None:
        sqtDelInstFac = "DELETE FROM FacIn WHERE InstitutionID = (?) AND FacultyID = (?)"
        con.execute(sqtDelInstFac, (instID, facID))

    # Check if faculty exists
    sqlQueryCheckExist = "SELECT * FROM Faculties WHERE FacultyName = (?)"
    sqlRes = con.execute(sqlQueryCheckExist, (faculty_test, ))
    record = sqlRes.fetchone()

    # If faculty exists delete it
    if record != None:
        sqlDelFac = "DELETE FROM Faculties WHERE FacultyID = (?)"
        con.execute(sqlDelFac, (facID, ))

    # Check if institution exists
    sqlQueryCheckExist = "SELECT * FROM Institutions WHERE InstitutionName = (?)"
    sqlRes = con.execute(sqlQueryCheckExist, (institution_test, ))
    record = sqlRes.fetchone()

    # If institution exists create it
    if record != None:
        sqtDelInst = "DELETE FROM Institutions WHERE InstitutionID = (?)"
        con.execute(sqtDelInst, (instID, ))

    # Check if additional faculty exists
    sqlQueryCheckExist = "SELECT * FROM Faculties WHERE FacultyName = (?)"
    sqlRes = con.execute(sqlQueryCheckExist, (newFacTest, ))
    record = sqlRes.fetchone()

    # If faculty exists create it
    if record != None:
        sqtDelInst = "DELETE FROM Faculties WHERE FacultyID = (?)"
        con.execute(sqtDelInst, (record[0], ))

    # Commit the changes in users table
    con.commit()

    # CLose connection to DB
    con.close()
Ejemplo n.º 5
0
def register():
    # Load all institutions
    institutions = []

    con = sqlite3.connect(current_app.config['DB_NAME'])

    # Preprare query
    sqlQueryInstitutions = "SELECT * FROM Institutions"

    # Run the query and save result
    sqlRes = con.execute(sqlQueryInstitutions)

    # Run over the lines of the result and append to list
    for line in sqlRes:
        institutions.append([line[0], line[1]])

    # Close the connection to the database
    con.close()

    # Check if user already logged in
    if ('username' in session):
        return redirect('/')
    # If method post selected then register the user
    if (request.method == "POST"):
        # connect to db and check if username taken
        con = sqlite3.connect(current_app.config['DB_NAME'])
        sqlQueryCheckExist = "SELECT * FROM Users WHERE UserName = (?)"
        sqlRes = con.execute(sqlQueryCheckExist, (request.form["username"], ))
        record = sqlRes.fetchone()

        # Create user object
        newUser = User(request.form["username"],
                       request.form["fName"],
                       request.form["lName"],
                       request.form["password"],
                       request.form["institution"],
                       request.form["faculty"],
                       request.form["year"],
                       email=request.form["email"])

        # Check if the user is not already registered!
        if (record == None):
            # Validate the user
            valMessage = newUser.validateUser()

            valMessage = valMessage.replace('\n', '<br>')
            valMessage = Markup(valMessage)

            # Check if user is valid
            if (valMessage != ""):
                return render_template('register.html',
                                       massage=valMessage,
                                       institutions=institutions)

            # Insert the user into the table of users
            sqlQueryRegister = "INSERT INTO Users VALUES (?,?, ?, ?, ?, ?, ?, 0, 0, ?)"
            con.execute(
                sqlQueryRegister,
                (newUser.getUsername(), newUser.getFName(), newUser.getLName(),
                 encryptPassword(newUser.getPassword()),
                 newUser.getInstitutionID(), newUser.getFacultyID(),
                 newUser.getStudyYear(), newUser.getEmail()))

            # Commit the changes in users table
            con.commit()

            # Create message
            massage = "User registered successfully!"

            # Add the user into the session variable
            session['username'] = newUser.getUsername()
        else:
            massage = "Username already taken please choose another!"
            return render_template('register.html',
                                   massage=massage,
                                   institutions=institutions)

        # Close the database connection
        con.close()

        return redirect('/')
    # Load and prepare the page
    else:
        return render_template('register.html',
                               massage="Please register",
                               institutions=institutions)
Ejemplo n.º 6
0
def init():
    with sqlite3.connect(DB_NAME) as con:
        # setup
        con.execute("DELETE FROM Faculties")
        con.execute("DELETE FROM Institutions")
        con.execute("DELETE FROM Lecturers")
        con.execute("DELETE FROM Courses")
        con.execute("DELETE FROM Files")
        con.execute("DELETE FROM Files")
        con.execute("DELETE FROM FacIn")
        con.execute("DELETE FROM Users")
        con.execute("DELETE FROM Notification")
        # 1)
        con.execute("INSERT INTO Institutions VALUES (?, ?)", (1, "A"))
        con.execute("INSERT INTO Faculties VALUES (?, ?)", (11, "math"))

        con.execute("INSERT INTO Lecturers VALUES (?, ?, ?, ?)", (1111, "Moshe", 11, 1))
        con.execute(
            "INSERT INTO Courses VALUES (?, ?, ?, ?)", (111, "Calculus", 1111, 2021)
        )
        con.execute("INSERT INTO FacIn VALUES (?, ?)", (1, 11))
        # 2)
        con.execute("INSERT INTO Faculties VALUES (?, ?)", (22, "art"))
        con.execute("INSERT INTO Institutions VALUES (?, ?)", (2, "B"))
        con.execute("INSERT INTO Lecturers VALUES (?, ?, ?, ?)", (2222, "Sarah", 22, 2))
        con.execute(
            "INSERT INTO Courses VALUES (?, ?, ?, ?)",
            (222, "study of drawing", 2222, 2021),
        )
        con.execute("INSERT INTO FacIn VALUES (?, ?)", (2, 22))
        con.execute(
            "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (
                "admin",
                "nadmin",
                "ladmin",
                encryptPassword("admin"),
                1,
                11,
                2021,
                1,
                0,
                "*****@*****.**",
            ),
        )
        con.execute(
            "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (
                "user1",
                "userone",
                "ulnone",
                encryptPassword("userone"),
                1,
                11,
                2021,
                0,
                0,
                "[email protected]",
            ),
        )
        con.execute(
            "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (
                "user2",
                "usertwo",
                "ulntwo",
                encryptPassword("usertwo"),
                2,
                22,
                2021,
                0,
                0,
                "[email protected]",
            ),
        )
        con.execute("INSERT INTO Types(Type) VALUES (?) ", ("Lecture",))
        con.execute("INSERT INTO Types(Type) VALUES (?) ", ("Lab",))
        con.execute("INSERT INTO Types(Type) VALUES (?) ", ("Exam",))

    os.mkdir("storage")
    os.mkdir("storage/backup")
Ejemplo n.º 7
0
def init():
    with sqlite3.connect(DB_NAME) as con:
        # setup
        con.execute("DELETE FROM Faculties")
        con.execute("DELETE FROM OldFiles")
        con.execute("DELETE FROM Institutions")
        con.execute("DELETE FROM Lecturers")
        con.execute("DELETE FROM Courses")
        con.execute("DELETE FROM Files")
        con.execute("DELETE FROM FacIn")
        con.execute("DELETE FROM Users")
        con.execute("DELETE FROM Notification")
        con.execute("DELETE FROM Reports")
        # 1)
        con.execute("INSERT INTO Institutions VALUES (?, ?)", (1, "A"))
        con.execute("INSERT INTO Faculties VALUES (?, ?)", (11, "math"))

        con.execute("INSERT INTO Lecturers VALUES (?, ?, ?, ?)", (1111, "Moshe", 11, 1))
        con.execute(
            "INSERT INTO Courses VALUES (?, ?, ?, ?)", (111, "Calculus", 1111, 2021)
        )
        con.execute("INSERT INTO FacIn VALUES (?, ?)", (1, 11))
        con.execute(
            "INSERT INTO Files VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (
                1,
                "user1",
                "F1.txt",
                "title-math",
                "special number",
                date(2021, 1, 1),
                date(2021, 1, 1),
                1,
                11,
                111,
                1,
                "Lecture",
            ),
        )
        # 2)
        con.execute("INSERT INTO Faculties VALUES (?, ?)", (22, "art"))
        con.execute("INSERT INTO Institutions VALUES (?, ?)", (2, "B"))
        con.execute("INSERT INTO Lecturers VALUES (?, ?, ?, ?)", (2222, "Sarah", 22, 2))
        con.execute(
            "INSERT INTO Courses VALUES (?, ?, ?, ?)",
            (222, "study of drawing", 2222, 2021),
        )
        con.execute("INSERT INTO FacIn VALUES (?, ?)", (2, 22))
        con.execute(
            "INSERT INTO Files VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (
                2,
                "Moshe",
                "F2.txt",
                "titile-sokal",
                "sokal-affair",
                date(2021, 1, 1),
                date(2021, 1, 1),
                2,
                22,
                222,
                1,
                "Lecture",
            ),
        )
        con.execute(
            "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (
                "admin",
                "nadmin",
                "ladmin",
                encryptPassword("admin"),
                1,
                11,
                2021,
                1,
                0,
                "*****@*****.**",
            ),
        )
        con.execute(
            "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (
                "user1",
                "userone",
                "ulnone",
                encryptPassword("userone"),
                1,
                11,
                2021,
                0,
                0,
                "[email protected]",
            ),
        )
        con.execute(
            "INSERT INTO Users VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (
                "user2",
                "usertwo",
                "ulntwo",
                encryptPassword("usertwo"),
                2,
                22,
                2021,
                0,
                0,
                "[email protected]",
            ),
        )
        con.execute("INSERT INTO Types(Type) VALUES (?) ", ("Lecture",))
        con.execute("INSERT INTO Types(Type) VALUES (?) ", ("Lab",))
        con.execute("INSERT INTO Types(Type) VALUES (?) ", ("Exam",))

    try:
        os.mkdir("storage")
    except FileExistsError as e:
        pass

    try:
        os.mkdir(os.path.join("storage","backup"))
    except FileExistsError as e:
        pass
    
    shutil.copy("Tests/test_storage_1/1.txt", "storage/1.txt")
    shutil.copy("Tests/test_storage_1/2.txt", "storage/2.txt")