Пример #1
0
    def loginUser(self, email, password):
        # Get user from database and load into manager
        # Return None is user doesnt exist
        userID = self._db.checkUserCred(email, password, use_bcrypt=True)
        if not userID > 0:
            return None
        try:
            _, userFN, userLN, email, contacts, prefs = \
                self._db.getUser(userID=userID)
        except:
            return None

        # Load preferences
        prefs = json.loads(prefs) if prefs else None
        user = User(userID, userFN, userLN, email, prefs)
        prefs = user.getPreferences()
        self._users[userID] = user

        # Add contacts to users
        try:
            contacts = json.loads(contacts)
            for email in contacts.keys():
                user.addContact(email, contacts[email]['name'])

                for group in contacts[email]['groups']:
                    user.addContact(email, groupName=group)
        except:
            pass


        # Get events from database
        events = self._db.getUserEvents(userID)
        if events == -1 or not events: events = []

        calendars = {cal.getName(): cal for cal in user.getCalendars()}
        for e in events:
            eventID = e[0]
            cal = e[6]
            # Load calendar if not already loaded
            if cal not in calendars.keys():
                calendars[cal] = Calendar(cal, prefs['default_colour'])

            # Load event
            if eventID not in self._events.keys():
                if not self.loadEvent(eventID): continue
            calendars[cal].addEvent(self._events[eventID])

        # Load invites
        invites = self._db.getInvitesByUser(userID)
        if invites == -1 or not invites: invites = []

        for i in invites:
            eventID, _, status, cal = i
            if eventID not in self._events.keys():
                eventID = e[0]
                cal = e[6]
                # Load calendar if not already loaded
                if cal not in calendars.keys():
                    calendars[cal] = Calendar(cal, prefs['default_colour'])

                # Load event
                if eventID not in self._events.keys():
                    if not self.loadEvent(eventID): continue
                calendars[cal].addInvite(self._events[eventID], status)

        # Get notifications from database
        notifs = self._db.getNotifications(userID)
        if notifs == -1 or not notifs: notifs = []

        for n in notifs:
            eventID, senderID, notifType = n
            # Load event if not already loaded
            if eventID not in self._events.keys():
                if not self.loadEvent(eventID): continue

            senderEmail = ""
            if senderID in self._users.keys():
                senderEmail = self._users[senderID].getEmail()
            else:
                sender = self._db.getUser(userID=senderID)
                if sender and sender != -1: senderEmail = sender[3]

            if senderEmail:
                user.addNotification(event=self._events[eventID],
                    notifType=notifType, senderEmail=senderEmail)

            # Delete notification from database
            self._db.deleteNotification(eventID, senderID, userID, notifType)

        for c in calendars.values():
            user.addCalendar(c)

        user.setAuthenticated()
        return user
Пример #2
0
class TestUser():
    @pytest.fixture()
    def fixture(self):
        self.user = User(1, "Derrick", "Foo", "*****@*****.**", "abc123")
        self.user1 = User(2, "Zainab", "Alasadi", "*****@*****.**",
                          "abc123***")

        self.event = Event(1, self.user, "COMP4920 Meeting", "Standup",
                           datetime.datetime(2017, 11, 28, 22, 45),
                           datetime.datetime(2017, 11, 28, 23, 45), "Work",
                           "Work")
        self.event1 = Event(2, self.user, "21st Birthday",
                            "Bday party at Sydney",
                            datetime.datetime(2017, 11, 28, 22, 45),
                            datetime.datetime(2017, 11, 28, 23,
                                              45), "Work", "Work")
        self.event_edit = Event(1, self.user, "COMP4920 Meeting 2.0", "Online",
                                datetime.datetime.now(),
                                datetime.datetime.now(), "Personal", "Work")
        self.workCal = Calendar("Work", "red", self.user)
        self.personalCal = Calendar("Personal", "blue", self.user1)
        self.event.setCalendar(self.workCal)

        self.event.addInvitee(self.user1)
        self.workCal.addEvent(self.event)
        self.user1.addCalendars(self.personalCal)

    def test_user(self, fixture):
        assert (self.user.getID() == 1)
        assert (self.user.getFirstName() == "Derrick")
        assert (self.user.getLastName() == "Foo")
        assert (self.user.getEmail() == "*****@*****.**")
        assert (self.user.validate("abc123") == True)
        assert (len(self.user.getCalendars()) == 0)
        assert (len(self.user.getContacts()) == 0)
        assert (len(self.user.getGroups()) == 0)

    def test_accept_invite(self, fixture):
        assert (len(self.user1.getNotifications()) == 1)
        assert (len(self.personalCal.getEvents()) == 0)
        for notif in self.user1.getNotifications():
            self.user1.acceptInvite(notif, self.personalCal)
        assert (len(self.user1.getNotifications()) == 0)
        assert (len(self.personalCal.getEvents()) == 1)

    def test_maybe_invite(self, fixture):
        for notif in self.user1.getNotifications():
            self.user1.maybeInvite(notif, self.personalCal)
        assert (len(self.user1.getNotifications()) == 0)
        assert (len(self.personalCal.getEvents()) == 1)
        assert (len(self.user.getNotifications()) == 1)
        for notif in self.user.getNotifications():
            assert (notif.getNotifType() == 'maybe_invite')

    def test_decline_invite(self, fixture):
        for notif in self.user1.getNotifications():
            self.user1.declineInvite(notif)
        assert (len(self.user1.getNotifications()) == 0)
        assert (len(self.personalCal.getEvents()) == 0)
        assert (len(self.user.getNotifications()) == 1)
        for notif in self.user.getNotifications():
            assert (notif.getNotifType() == 'declined_invite')

    def test_update_event(self, fixture):
        self.user.updateEvent(
            self.event, "Wobcke Fanclub", "Wobcke Rocks!",
            datetime.datetime.now() - datetime.timedelta(days=5),
            datetime.datetime.now() - datetime.timedelta(days=5),
            self.personalCal, "Fun")
        assert (self.event.getCategory() == "Fun")
        assert (self.event.getName() == "Wobcke Fanclub")
        assert (self.event.getDescription() == "Wobcke Rocks!")
        assert (self.event.getCalendar() == self.personalCal)

    def test_update_event_with_invitees(self, fixture):
        self.user1 = User(2, "Zainab", "Alasadi", "*****@*****.**",
                          "abc123***")
        self.workCal1 = Calendar("Work", "red", self.user1)
        self.user1.addCalendars(self.workCal1)

        self.workCal.addEvent(self.event)
        self.event.addInvitee(self.user1)

        assert (len(self.workCal.getEvents()) == 1)
        assert (len(self.personalCal.getEvents()) == 0)

        self.user.updateEvent(
            self.event, "Wobcke Fanclub", "Wobcke Rocks!",
            datetime.datetime.now() - datetime.timedelta(days=5),
            datetime.datetime.now() - datetime.timedelta(days=5),
            self.personalCal, "Fun")

        assert (len(self.workCal.getEvents()) == 0)
        assert (len(self.personalCal.getEvents()) == 1)

        assert (len(self.user1.getNotifications()) == 1)
        for notif in self.user1.getNotifications():
            assert (len(notif.getChanges()) == 6)

        self.user.updateEvent(
            self.event, "Wobcke Fanclub", "Wobcke Rocks!",
            datetime.datetime.now() - datetime.timedelta(days=5),
            datetime.datetime.now() - datetime.timedelta(days=5),
            self.personalCal, "Fun")

        assert (len(self.workCal.getEvents()) == 0)
        assert (len(self.personalCal.getEvents()) == 1)

        assert (len(self.user1.getNotifications()) == 2)
        for notif in self.user1.getNotifications():
            assert (len(notif.getChanges()) == 6 or 2)
Пример #3
0
class TestCalendar():
    @pytest.fixture()
    def fixture(self):
        self.user = User(1, "Derrick", "Foo", "*****@*****.**", "abc123")
        self.event = Event(1, self.user, "COMP4920 Meeting", "Standup",
                           datetime.datetime.now(), datetime.datetime.now(),
                           "Work", "Work")
        self.eventCopy = Event(1, self.user, "COMP4920 Meeting", "Standup",
                               datetime.datetime.now(),
                               datetime.datetime.now(), "Personal", "Work")
        self.event1 = Event(2, self.user, "21st Birthday",
                            "Bday party at Sydney", datetime.datetime.now(),
                            datetime.datetime.now(), "Work", "Work")
        self.event_edit = Event(1, self.user, "COMP4920 Meeting 2.0", "Online",
                                datetime.datetime.now(),
                                datetime.datetime.now(), "Personal", "Work")
        self.workCal = Calendar("Work", "red", self.user)
        self.personalCal = Calendar("Personal", "blue", self.user)
        self.user.addCalendars(self.workCal)

        self.event.setCalendar(self.workCal)

    def test_add_event(self, fixture):
        assert (self.workCal.addEvent(self.event))
        assert (len(self.user.getCalendars()) == 1)
        assert (len(self.workCal.getEvents()) == 1)

    def test_add_same_event_same_calendar(self, fixture):
        assert (self.workCal.addEvent(self.event))
        assert (len(self.workCal.getEvents()) == 1)
        assert not (self.workCal.addEvent(self.event))
        assert (len(self.workCal.getEvents()) == 1)

    def test_add_same_event_diff_calendars(self, fixture):
        assert (self.workCal.addEvent(self.event))
        assert (len(self.workCal.getEvents()) == 1)
        assert (self.personalCal.addEvent(self.event))
        self.user.addCalendars(self.personalCal)
        assert (len(self.personalCal.getEvents()) == 1)

    def test_add_multiple_events(self, fixture):
        assert (self.workCal.addEvent(self.event))
        assert (len(self.workCal.getEvents()) == 1)
        assert (self.workCal.addEvent(self.event1))
        assert (len(self.workCal.getEvents()) == 2)

    def test_delete_events_when_none_exist(self, fixture):
        assert not (self.workCal.deleteEvent(self.event))

    def test_delete_event(self, fixture):
        assert (self.workCal.addEvent(self.event))
        assert (len(self.workCal.getEvents()) == 1)
        assert (self.workCal.deleteEvent(self.event))
        assert (len(self.workCal.getEvents()) == 0)
        assert not (self.workCal.deleteEvent(self.event))

    def test_delete_event_diff_calendars(self, fixture):
        assert (self.workCal.addEvent(self.event))
        assert (len(self.workCal.getEvents()) == 1)
        assert (self.personalCal.addEvent(self.eventCopy))
        self.user.addCalendars(self.personalCal)
        assert (len(self.personalCal.getEvents()) == 1)

        self.user.deleteEvent(self.event)
        assert (len(self.personalCal.getEvents()) == 1)
        assert (len(self.workCal.getEvents()) == 0)

    def test_delete_event_all_calendars(self, fixture):
        assert (self.workCal.addEvent(self.event))
        assert (len(self.workCal.getEvents()) == 1)
        assert (self.personalCal.addEvent(self.event))
        self.user.addCalendars(self.personalCal)
        assert (len(self.personalCal.getEvents()) == 1)

        self.user.deleteEvent(self.event)
        assert (len(self.personalCal.getEvents()) == 0)
        assert (len(self.workCal.getEvents()) == 0)

    def test_delete_all_when_none(self, fixture):
        self.user.deleteEvent(self.event)
        assert (len(self.personalCal.getEvents()) == 0)
        assert (len(self.workCal.getEvents()) == 0)

    def test_delete_event_one_calendar(self, fixture):
        assert (self.workCal.addEvent(self.event))
        assert (len(self.workCal.getEvents()) == 1)
        self.event.setCalendar(self.workCal)
        assert (self.personalCal.addEvent(self.eventCopy))
        self.user.addCalendars(self.personalCal)
        self.eventCopy.setCalendar(self.personalCal)
        assert (len(self.personalCal.getEvents()) == 1)

        assert (self.user.deleteEventOneCalendar(self.event))
        assert (len(self.personalCal.getEvents()) == 1)
        assert (len(self.workCal.getEvents()) == 0)

    def test_delete_one_when_none(self, fixture):
        self.user.deleteEventOneCalendar(self.event)
        assert (len(self.personalCal.getEvents()) == 0)
        assert (len(self.workCal.getEvents()) == 0)