Ejemplo n.º 1
0
 def test_delete_user_success(self):
     post_user("megan", self.birthday)
     r = delete_user("megan")
     self.assertEqual(r.status_code, 200)
Ejemplo n.º 2
0
 def test_get_user_not_there(self):
     delete_user("harrypotter")
     r = get_user("harrypotter")
     self.assertEqual(r.status_code, 404)
Ejemplo n.º 3
0
 def test_delete_user_not_there(self):
     r = delete_user("lauren")
     self.assertEqual(r.status_code, 404)
Ejemplo n.º 4
0
 def test_add_user_success(self):
     delete_user("emily")
     r = post_user("emily", self.birthday)
     self.assertEqual(r.status_code, 200)
Ejemplo n.º 5
0
def chatroom(room_id):
    room_id = str(room_id)
    """Part of the attempt to implement push notifications:"""
    # listening_thread = threading.Thread(target=listening(room_id))
    # listening_thread.start()
    """Timed thread:"""
    # timeout = 5
    # listen_timed = threading.Timer(timeout, listening(room_id))
    # listen_timed.start()
    # listen_timed.cancel()
    """Another solution we found was using signal which would have allowed us to set a timer on a while loop
    Issue: it only works on Linux."""

    print(f"You are now in room {room_id}")
    print(
        "For help, type '--help'.\nTo see new messages press enter/return without typing anything."
    )
    readmessages = []
    """ Loop that allows for messages from the user (input) to be continually accept.
    Before sending any messages, main.py is polled for any messages that might not yet have been read. """
    while True:
        try:
            response = get_all_messages(room_id, username)
            """ Automatic polling of the server/push notification attempt (related to def enter()):
            Adding a background scheduler to press enter every 10 seconds to update the chat and see new messages.
                Polling every 10 seconds for new messages because the input is a blocking call stopping us
                from updating the get request for all messages in the chatroom.
                This was our way of trying push notifications - instead of pressing enter it could also be sending a message like "A new message has appeared in your chat room, press enter to see it."
            The problem with this is that if you're running two terminals on the same computer it can only emulate one enter keypress and it will continue doing it untill you do a press of enter in
            The initial terminal the background process started in. So it will continue pressing enter every 10 seconds in any active window, even outside of the program.
            We decided not to use it because of this fault in the process in combination with our program. Having it continually press enter untill it does it in the right window isnt a good way to deal with it."""

            # initialmessage = response.json()
            # scheduling = False
            if response.status_code == 200:
                """Unused code for the background scheduler pressing enter (through the enter function)"""
                # if len(initialmessage) > len(readmessages):
                #     scheduler = BackgroundScheduler()
                #     scheduler.start()
                #     scheduling = True
                #     scheduler.add_job(enter, 'interval', seconds=10)

                for i in range(len(readmessages), len(response.json())):
                    user = response.json()[i]["username"]
                    msg = response.json()[i]["message"]
                    if user != username:
                        print(user + f" to room {room_id}: " + msg)
                readmessages = response.json()
            message_input = input(username + " to room " + room_id + ": ")
            message_input = message_input.strip()
            # if scheduling == True:
            #     scheduler.shutdown()
            #     scheduling = False
            # timer_thread.cancel()
            """Handles different predefined inputs (commands) from the user."""
            if message_input == "--exit":
                """Allows user to go back to the main terminal. User is still a room user."""
                print(f"----- You have exited room {room_id} ------")
                print("You are back in the main terminal")
                break

            elif message_input == "--help":
                print(
                    "Type '--help' to show this help prompt\n" +
                    "Press enter/return without typing anything to update the chatroom to see new messages.\n"
                    +
                    "Type '--showmessages' to show all messages in this chatroom\n"
                    +
                    "Type '--showmymessages' to see your messages in this chatroom\n"
                    +
                    "Type '--exit' to leave the chatroom and go back to the main terminal\n"
                    +
                    "Type '--removeme' to remove yourself from this chatroom")

            elif message_input == "--showmessages":
                """Shows all messages sent to the room where the user currently is."""
                response = get_all_messages(room_id, username)
                print(response.json())

            elif message_input == "--showmymessages":
                """Shows messages send by user to the room where they currently are."""
                response = get_messages(room_id, username, username)
                print("Messages in room " + str(room_id) + " from " +
                      username + ":")
                print(response.json())

            elif message_input == "--removeme":
                """Removes user from list of roomusers of the room and exits to the main terminal"""
                answer = input(
                    "Are you sure you want to REMOVE yourself from this room? y/[N] "
                )
                answer = answer.lower()
                if answer == 'y' or joinmessage == 'yes':
                    delete_roomuser(room_id, username, username)
                    break
                else:
                    print("If it's not a yes, then you stay")
                    continue

            else:
                #"""If not one of the previously defined messages..."""
                if len(message_input) < 1:
                    continue
                else:
                    #"""...and the message is not empty, then add to list of messages in the room."""
                    add_message(room_id, username, message_input, username)

        except (EOFError, KeyboardInterrupt):
            print(
                "\nYou have chosen to leave the room, but you are still in its userlist. \nPress ctrl c again to leave the entire program and delete your user."
            )
            break
        except:
            print(
                "\nThere was an unknown error. You are being deleted and logged out."
            )
            response = delete_user(username, username)
            sys.exit()
Ejemplo n.º 6
0
            elif response.status_code == 404:
                print("Couldn't find room with room_id " + roomchoice)
            elif response.status_code == 401:
                print(
                    "You must be a part of this room to query messages from it"
                )
            else:
                print("Couldn't show messages from room " + roomchoice)

        elif message == '--exit':
            """Exits the program and deletes current user."""
            print("Are you sure you want to exit? This will DELETE your user!")
            answer = input("y/[N]: ")
            answer = answer.lower()
            if answer == "y" or answer == "yes":
                response = delete_user(username, username)
                sys.exit("You have exited the program. Goodbye.")
            elif answer == "n" or answer == "no":
                continue
            else:
                print(
                    "Invalid input. You will be taken back to the main terminal."
                )
        else:
            print("Not a command, dummy. If you need help, type '--help'")

    except (EOFError, KeyboardInterrupt):
        print(
            "\nYou have chosen to leave the program and that is your loss. \nYour user is being deleted as revenge."
        )
        response = delete_user(username, username)
Ejemplo n.º 7
0
 def setUpClass(cls):
     r1 = delete_user("lucy")
     r2 = post_user("lucy", "11/20/1996")
     cls.user_id = r2.json()["user_id"]
     cls.very_high_number = 10000000
Ejemplo n.º 8
0
 def tearDownClass(cls):
     delete_user("lucy")