Module backend.tests

This file contains tests for the API requests within JobTrackr. The tests evalute both the proper method of using the function as well as a possible way the user could deviate from the expected use case.

Classes

class FlaskTest (methodName='runTest')

A class whose instances are single test cases.

By default, the test code itself should be placed in a method named 'runTest'.

If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.

Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.

If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.

When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

Expand source code
class FlaskTest(unittest.TestCase):

    def setUp(self):
        # Insert test data here
        hashed = bcrypt.hashpw(
            "12345678".encode("utf-8"), bcrypt.gensalt())
        user_input = {"name": "dhrumil",
                      "email": "dhrumilshah1234@gmail.com", "password": hashed}
        UserRecords.insert_one(user_input)
        application = {
            "_id": ObjectId("638eb81bff4164e60179bab2"),
            "email": "dhrumilshah1234@gmail.com",
            "companyName": "Lexis Nexis",
            "jobTitle": "Software Engineer",
            "jobId": "12345",
            "description": "Not much",
            "url": "https://www.google.com",
            "date": None,
            "status": "Accepted",
            "image": None
        }
        Questions.insert_one({"email": "dhrumilshah1234@gmail.com", "_id": ObjectId("638bafe50012ef455196cc6e"),
                              "question": "aa", "answer": "bb"})
        Applications.insert_one(application)
        UserRecords.delete_one({"email": "rrangar@ncsu.edu"})
        Files.insert_one({"_id": ObjectId("66f5d5ed19e1859f9d89676f"),
                          "email": "dhrumilshah1234@gmail.com",
                          "filename": "1234568--;--testing.pdf"})

    def tearDown(self):
        # Clear the mock data after each test
        UserRecords.delete_one(
            {"name": "dhrumil", "email": "dhrumilshah1234@gmail.com"})
        Applications.delete_one({"_id": ObjectId("638eb81bff4164e60179bab2"),
                                 "email": "dhrumilshah1234@gmail.com"})
        Applications.delete_many({"email": "dhrumilshah1234@gmail.com"})
        Questions.delete_many({"email": "dhrumilshah1234@gmail.com"})
        Files.delete_one({"_id": ObjectId("66f5d5ed19e1859f9d89676f")})

    def testLogin(self):
        tester = app.test_client(self)
        response = tester.post(
            "/login", json={"email": "dhrumilshah1234@gmail.com", "password": "12345678"})
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testWrongLogin(self):
        tester = app.test_client(self)
        response = tester.post(
            "/login", json={"email": "xyz@ncsu.edu", "password": "jytfyjtyj"})
        statuscode = response.status_code
        # User account not found
        self.assertEqual(statuscode, 400)

    def testWrongLoginPassword(self):
        tester = app.test_client(self)
        response = tester.post(
            "/login", json={"email": "dhrumilshah1234@gmail.com", "password": "jytfyjtyj"})
        statuscode = response.status_code
        # User password not correct
        self.assertEqual(statuscode, 400)

    def testRegister(self):
        tester = app.test_client(self)
        req = {}
        req["firstName"] = "Rahul"
        req["lastName"] = "RK"
        req["email"] = "rrangar@ncsu.edu"
        req["password"] = "12345678"
        req["confirmPassword"] = "12345678"
        urlToSend = "/register"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        # New user created
        self.assertEqual(statuscode, 200)

    def testWrongRegisterExistingEmail(self):
        tester = app.test_client(self)
        req = {}
        req["firstName"] = "Rahul"
        req["lastName"] = "RK"
        req["email"] = "dhrumilshah1234@gmail.com"
        req["password"] = "12345678"
        req["confirmPassword"] = "12345678"
        urlToSend = "/register"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 400)

    def testWrongRegisterNoMatchingPasswor(self):
        tester = app.test_client(self)
        req = {}
        req["firstName"] = "Rahul"
        req["lastName"] = "RK"
        req["email"] = "rrangar@ncsu.edu"
        req["password"] = "12345678"
        req["confirmPassword"] = "123456789"
        urlToSend = "/register"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 400)

    def testdeletewrongApplication(self):
        tester = app.test_client(self)
        response = tester.post("/delete_application", json={
                               "email": "dhrumilshah1234@gmail.com", "_id": "63800dfd2bf155063a7afbd9"})
        statuscode = response.status_code
        self.assertEqual(statuscode, 400)

    def testViewApplication(self):
        tester = app.test_client(self)
        email = "dhrumilshah1234@gmail.com"
        urlToSend = f"/view_applications?email={email}"
        response = tester.get(urlToSend)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testViewQuestions(self):
        tester = app.test_client(self)
        email = "dhrumilshah1234@gmail.com"
        urlToSend = f"/view_questions?email={email}"
        response = tester.get(urlToSend)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testdeletewrongQuestions(self):
        tester = app.test_client(self)
        response = tester.post(
            "/delete_question", json={"email": "dhrumilshah1234@gmail.com", "_id": "63800dfd2bf155063a7afbd9"})
        statuscode = response.status_code
        self.assertEqual(statuscode, 400)

    def testAddApplication(self):
        tester = app.test_client(self)
        req = {}
        req["companyName"] = "Edusetu"
        req["jobTitle"] = "Test"
        req["email"] = "dhrumilshah1234@gmail.com"
        req["jobId"] = "12345678"
        req["url"] = "www.google.com"
        req["status"] = "applied"
        urlToSend = "/add_application"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testAddQuestion(self):
        tester = app.test_client(self)
        req = {}
        req["email"] = "dhrumilshah1234@gmail.com"
        req["question"] = "Why do you want to apply to this Job?"
        req["answer"] = "Because i want money and you are Hiring"
        urlToSend = "/add_question"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testModifyApplication(self):
        tester = app.test_client(self)
        req = {
            "companyName": "a",
            "jobTitle": "a",
            "jobId": "a",
            "description": "a",
            "url": "b",
            "date": "2022-12-28T03:33:43.737Z",
            "status": "inReview",
            "_id": "638eb81bff4164e60179bab2",
            "email": "dhrumilshah1234@gmail.com",
            "image": ""
        }
        urlToSend = "/modify_application"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testModifyQuestion(self):
        tester = app.test_client(self)
        req = {
            "question": "Q1",
            "answer": "A1",
            "_id": "638bafe50012ef455196cc6e",
            "email": "dhrumilshah1234@gmail.com"
        }
        urlToSend = "/modify_question"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testWrongEmailModifyQuestion(self):
        tester = app.test_client(self)
        req = {
            "question": "Q1",
            "answer": "A1",
            "_id": "638bafe50012ef455196cc6e",
            "email": "a@a.com"
        }
        urlToSend = "/modify_question"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 400)

    def testWrongModifyQuestion(self):
        tester = app.test_client(self)
        req = {
            "question": "Q1",
            "answer": "A1",
            "_id": "IDONTEXIST",
            "email": "dhrumilshah1234@gmail.com"
        }
        urlToSend = "/modify_question"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        # Since the ID doesnt exist.
        self.assertEqual(statuscode, 400)

    def testWrongModifyApplication(self):
        tester = app.test_client(self)
        req = {
            "companyName": "k",
            "jobTitle": "jl",
            "jobId": "nln",
            "description": "lkn",
            "url": "lknl",
            "date": "2022-12-02T21:26:03.739Z",
            "status": "interview",
            "_id": "IDONTEXIST",
            "email": "dhrumilshah1234@gmail.com"
        }
        urlToSend = "/modify_application"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 400)

    def testWrongModifyApplicationWrongEmail(self):
        tester = app.test_client(self)
        req = {
            "companyName": "a",
            "jobTitle": "a",
            "jobId": "a",
            "description": "a",
            "url": "b",
            "date": "2022-12-28T03:33:43.737Z",
            "status": "inReview",
            "_id": "638eb81bff4164e60179bab2",
            "email": "a@gmail.com",
            "image": ""
        }
        urlToSend = "/modify_application"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 400)

    def testModifyApplicationNoDate(self):
        tester = app.test_client(self)
        req = {
            "companyName": "Qorvo",
            "jobTitle": "Software Engineer",
            "jobId": "122881",
            "description": "lkn",
            "url": "",
            "date": None,
            "status": "interview",
            "_id": "638eb81bff4164e60179bab2",
            "email": "dhrumilshah1234@gmail.com",
            "image": "www.google.com"
        }
        urlToSend = "/modify_application"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testModifyApplicationMissingImage(self):
        tester = app.test_client(self)
        req = {
            "companyName": "Qorvo",
            "jobTitle": "Software Engineer",
            "jobId": "122881",
            "description": "lkn",
            "url": "",
            "date": "2022-12-02T21:26:03.739Z",
            "status": "interview",
            "_id": "638eb81bff4164e60179bab2",
            "email": "dhrumilshah1234@gmail.com",
        }
        urlToSend = "/modify_application"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testWrongModifyApplicationStatusAndURL(self):
        tester = app.test_client(self)
        req = {
            "companyName": "k",
            "jobTitle": "jl",
            "jobId": "nln",
            "description": "lkn",
            "url": "lknl",
            "date": "2022-12-02T21:26:03.739Z",
            "status": "a",
            "_id": "638eb81bff4164e60179bab2",
            "email": "dhrumilshah1234@gmail.com"
        }
        urlToSend = "/modify_application"
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 400)

    def testviewFiles(self):
        tester = app.test_client(self)
        email = "testing@gmail.com"
        urlToSend = f"/view_files?email={email}"
        response = tester.get(urlToSend)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testViewFilesWithContent(self):
        tester = app.test_client(self)
        email = "dhrumilshah1234@gmail.com"
        urlToSend = f"/view_files?email={email}"
        response = tester.get(urlToSend)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)
        self.assertTrue(len(json.loads(response.data)["files"]) == 1)

    def testWrongViewFilesWrongEmail(self):
        tester = app.test_client(self)
        email = "a@gmail.com"
        urlToSend = f"/view_files?email={email}"
        response = tester.get(urlToSend)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)
        self.assertTrue(len(json.loads(response.data)["files"]) == 0)

    def testEmptyFiles(self):
        tester = app.test_client(self)
        email = "a@a.com"
        urlToSend = f"/view_files?email={email}"
        response = tester.get(urlToSend)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testGenerateCoverLetter(self):
        tester = app.test_client(self)
        email = "dhrumilshah1234@gmail.com"
        urlToSend = f"/generate_cv"
        req = {
            "email": email,
            "file": "",
            "context": "I want to be good at programaming",
            "job_desc": "We want embedded engineers."
        }
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testGenerateCoverLetterWithoutFile(self):
        tester = app.test_client(self)
        email = "dhrumilshah1234@gmail.com"
        urlToSend = f"/generate_cv"
        req = {
            "email": email,
            "context": "I want to be good at programaming",
            "job_desc": "We want embedded engineers."
        }
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

    def testResumeSuggestions(self):
        tester = app.test_client(self)
        email = "dhrumilshah1234@gmail.com"
        urlToSend = f"/resume_suggest"
        req = {
            "email": email,
            "file": "",
            "job_desc": "We want embedded engineers."
        }
        response = tester.post(urlToSend, json=req)
        statuscode = response.status_code
        self.assertEqual(statuscode, 200)

Ancestors

  • unittest.case.TestCase

Methods

def setUp(self)

Hook method for setting up the test fixture before exercising it.

def tearDown(self)

Hook method for deconstructing the test fixture after testing it.

def testAddApplication(self)
def testAddQuestion(self)
def testEmptyFiles(self)
def testGenerateCoverLetter(self)
def testGenerateCoverLetterWithoutFile(self)
def testLogin(self)
def testModifyApplication(self)
def testModifyApplicationMissingImage(self)
def testModifyApplicationNoDate(self)
def testModifyQuestion(self)
def testRegister(self)
def testResumeSuggestions(self)
def testViewApplication(self)
def testViewFilesWithContent(self)
def testViewQuestions(self)
def testWrongEmailModifyQuestion(self)
def testWrongLogin(self)
def testWrongLoginPassword(self)
def testWrongModifyApplication(self)
def testWrongModifyApplicationStatusAndURL(self)
def testWrongModifyApplicationWrongEmail(self)
def testWrongModifyQuestion(self)
def testWrongRegisterExistingEmail(self)
def testWrongRegisterNoMatchingPasswor(self)
def testWrongViewFilesWrongEmail(self)
def testdeletewrongApplication(self)
def testdeletewrongQuestions(self)
def testviewFiles(self)