def test_invalid_ticket_id_request(
         self, test_get):  # 404 Invalid Ticket ID Response
     api = APIRequestHandler()
     self.assertEqual(api.requestAPI(), False)
     self.assertEqual(
         api.getTicket('abcd'), -1
     )  # Invalid ticket ID 'abcd', fetches a response of -1 from getTicket
Exemplo n.º 2
0
 def __init__(self):
     self.view = AppView()  # An AppView instance being used by this class.
     self.api = APIRequestHandler(
     )  # An APIRequestHandler instance being used by this class
     self.input = ""  # Input given by user
     self.currID = 999  # A random ticket ID. This points to the ticket we are currently viewing.
     self.currPage = 999  # A random page number. This points to the current page we are viewing.
 def test_unauthorized_request(self, test_get):
     api = APIRequestHandler()
     self.assertEqual(api.requestAPI(), None)
     # testing that api.requestAPI returns None on 401 unauthorized request
     self.assertEqual(api.getTickets(), 1)
     # api.getTickets() returns 1, if api.requestAPI() returns None (user not authorized)
     self.assertEqual(api.getTicket('1'), 1)
 def test_api_unavailable_request(self, test_get):
     api = APIRequestHandler()
     self.assertEqual(api.requestAPI(), 1)
     # testing that api.requestAPI returns 1 on 503 API unavailable response
     self.assertEqual(api.getTickets(), 0)
     # Checking that api.getTickets() returns 0, if api.requestAPI() returns 1 (API unavailable)
     self.assertEqual(api.getTicket('1'), 0)
 def test_bad_request(self, test_get):
     api = APIRequestHandler()
     self.assertEqual(api.requestAPI(), 0)
     # testing that api.requestAPI returns 0 on general bad request (response status code = 400 in this case)
     self.assertEqual(api.getTickets(), False)
     # api.getTickets() returns 0, if api.requestAPI() returns 0 (bad request)
     self.assertEqual(api.getTicket('1'), False)
 def test_api_get_one(self, test_get):  # mocking api interaction, response status code = 200
     api = APIRequestHandler()
     ticket_raw = api.requestAPI(False, 2)  # Raw ticket with unformatted dates
     self.assertEqual(len(ticket_raw), 1)
     assert "ticket" in ticket_raw
     self.assertEqual(ticket_raw["ticket"]["id"], 2)
     ticket = api.getTicket(2)  # Processed ticket with formatted dates
     self.assertEqual(len(ticket), 1)
     assert "ticket" in ticket
     self.assertEqual(ticket["ticket"]["id"], 2)
 def test_api_get_all(self, test_get):  # mocking api interaction, response status code = 200
     api = APIRequestHandler()
     ticket_raw = api.requestAPI(True)  # Raw tickets with unformatted dates
     self.assertEqual(len(ticket_raw["tickets"]), 100)
     assert "tickets" in ticket_raw
     assert "next_page" in ticket_raw
     assert "previous_page" in ticket_raw
     assert "count" in ticket_raw
     ticket = api.getTickets()  # Processed tickets with formatted dates
     assert "tickets" in ticket
     assert "next_page" in ticket
     assert "previous_page" in ticket
     assert "count" in ticket
     self.assertEqual(len(ticket["tickets"]), 100)  # count = 101 in data.json, but actual length of json file = 100
Exemplo n.º 8
0
class AppController:
    def __init__(self):
        self.view = AppView()  # An AppView instance being used by this class.
        self.api = APIRequestHandler()  # An APIRequestHandler instance being used by this class
        self.input = ""  # Input given by user
        self.currID = 999  # A random ticket ID. This points to the ticket we are currently viewing.
        self.currPage = 999  # A random page number. This points to the current page we are viewing.

    def run(self):  # Driver method
        self.showMainMenu()

    def getInput(self):  # Prompts user for input
        self.input = input()

    def showMainMenu(self):  # Main menu view controller
        self.view.startMessage()
        while True:
            self.getInput()  # Get user input
            if self.input == "menu":  # Display app menu
                self.view.printMenu()
            elif self.input == '1':  # Show all tickets
                response = self.showTickets()
                if response is None:
                    self.view.displayInputMessage("\nEnter a command, to view command menu, type 'menu': ", 0)
                    # Display input prompt message
            elif self.input == '2':  # Show one ticket
                response = self.showTicket()
                if response is False:
                    self.view.displayInputMessage("\nEnter a command, to view command menu, type 'menu': ", 0)
                    # Display input prompt message
            elif self.input == 'q':  # Quit app
                sys.exit(self.view.quit())  # Print quit message and quit
            else:
                self.view.displayInputMessage(
                    "Invalid input, please enter a valid command. To view command options type 'menu': ",
                    1)  # Invalid user input for menu
            self.input = ""

    def showTickets(self):  # Controller method to display all tickets. Handles user inputs for paging requests
        try:
            self.view.fetchTickets("all")  # Fetching display message
            tickets = self.api.getTickets()  # Get all tickets
            assert tickets not in [-1, 0, 1, False]
            page = self.view.displayTickets(tickets, 1)
        except AssertionError as e:
            self.view.errorCode = self.api.errorCode
            if tickets == -1:  # No tickets on account
                self.view.displayBadRequest("No tickets on account to display")
            elif tickets == 1:  # Can't authenticate with API
                self.view.displayBadRequest("API authentication not permitted or invalid user credentials.")
            elif tickets == 0:  # API unavailable
                self.view.displayBadRequest("API unavailable. Please try again later")
            elif tickets is False:  # Other Bad Requests
                self.view.displayBadRequest("Unknown Bad Request")
            self.view.errorCode = None
            self.api.errorCode = None
            return None
        while True:
            self.getInput()
            if self.input == 'q':  # Quit app
                sys.exit(self.view.quit())  # Print quit message and quit
            elif self.input == "menu":  # Show menu
                self.view.printMenu()
                break
            elif self.input == "d":  # Page down
                page += 1
                page = self.view.displayTickets(tickets, page)
            elif self.input == "u":  # Page up
                page -= 1
                page = self.view.displayTickets(tickets, page)
            else:
                self.view.displayInputMessage(
                    "Page command error. 'd' to go down, 'u' to go up, 'menu' for menu and 'q' for quit: ", 1)
                # Invalid user input for ticket paging
            self.input = ""
            self.currPage = page
        return 0

    def showTicket(self):  # Controller method for displaying one ticket in view
        self.view.displayInputMessage("Enter the ticket ID: ", 0)  # Display ticket ID input message
        self.getInput()  # Get ticket ID
        ticketID = self.input
        self.input = ""
        try:
            self.view.fetchTickets(ticketID)  # Get ticket
            ticket = self.api.getTicket(ticketID)
            assert ticket not in [-1, 0, 1, False]
            self.view.displayTicket(ticket)  # Display ticket
            self.currID = int(ticketID)  # Current ticket ID
            return 0
        except AssertionError as e:
            self.view.errorCode = self.api.errorCode
            if ticket == 1:  # Can't authenticate with API
                self.view.displayBadRequest("API authentication not permitted or invalid user credentials.")
            elif ticket == -1:  # Ticket ID not valid
                self.view.displayBadRequest("The ticket ID you gave is not a valid ID")
            elif ticket == 0:  # API unavailable
                self.view.displayBadRequest("API unavailable. Please try again later")
            elif ticket is False:  # Other Bad Requests
                self.view.displayBadRequest("Unknown Bad Request")
            self.view.errorCode = None
            self.api.errorCode = None
            return False
 def test_date_formatting(self):  # test date is formatted correctly
     api = APIRequestHandler()
     updated, created = api.formatDates("2017-11-13T12:34:23Z",
                                        "2017-10-13T12:34:23Z")
     self.assertEqual(updated, "2017-11-13 12:34:23")
     self.assertEqual(created, "2017-10-13 12:34:23")
Exemplo n.º 10
0
 def test_date_formatting(self):  # test date is formatted correctly
     api = APIRequestHandler()
     updated, created = api.formatDates("2019-6-9T12:30:49Z", "2019-6-9T12:30:48Z")
     self.assertEqual(updated, "2019-6-9 12:30:49")
     self.assertEqual(created, "2019-6-9 12:30:48")