def test_get_calendar_for_object(self): calendar = Calendar(name='My Cal') calendar.save() rule = Rule() rule.save() calendar.create_relation(rule) result = Calendar.objects.get_calendar_for_object(rule) self.assertEqual(result.name, 'My Cal')
def test_get_calendar_for_object_with_more_than_one_calendar(self): calendar_1 = Calendar(name='My Cal 1') calendar_1.save() calendar_2 = Calendar(name='My Cal 2') calendar_2.save() rule = Rule() rule.save() calendar_1.create_relation(rule) calendar_2.create_relation(rule) with self.assertRaises(AssertionError): result = Calendar.objects.get_calendar_for_object(rule)
def test_get_calendar_for_object(self): user = User(username="******") user.save() calendar = Calendar(name='My Cal', slug='My Cal') calendar.save() calendar.create_relation(user) calendar_object = Calendar.objects.get_calendar_for_object(user) self.assertEqual(calendar_object.slug, 'My Cal') calendar = Calendar(name='My 2nd Cal') calendar.save() try: calendar.create_relation(user) except Exception as error: self.assertRaises( error, AssertionError("More than one calendars were found."))
def test_create_calendar_relation(self): user = User.objects.create(username='******', password='******') user.save() calendar = Calendar(name='Test Calendar') calendar.save() relation = calendar.create_relation(user) self.assertEqual(relation, None)
def save(self, *args, **kwargs): if not self.calendar_id: if self.pk: self.calendar = Calendar.objects.get_or_create_calendar_for_object( self) else: name = u'Calendar ' + datetime.now().strftime("%Y%m%d-%H%M%S") calendar = Calendar(name=name) calendar.slug = slugify(calendar.name) calendar.save() self.calendar = calendar obj = super(Alloggio, self).save(*args, **kwargs) calendar.create_relation(self) return obj return super(Alloggio, self).save(*args, **kwargs)
def create_flat(request): flat_name = request.data.get('name', None) if flat_name is None: return Response(data={ 'message': 'Flat name must be defined', }, status=400) flat = Flat.objects.create(name=flat_name) flat.users.add(request.user) flat.save() calendar = Calendar(name='flat_{0}'.format(flat.id)) calendar.save() calendar.create_relation(flat) return Response(data={ 'flat': FlatSerializer(flat).data, }, status=200)
class GroupAndMeetingTest(TestCase): def convert(self, ddatetime): if ddatetime: ddatetime = ddatetime.split(' ')[0] # print(ddatetime)\ ddatetime = ddatetime.split('+')[0] return datetime.datetime.strptime(ddatetime, '%Y-%m-%dT%H:%M:%S') return def setUp(self): self.client = Client() # User1: Not in any group or meeting self.User1 = User.objects.create_user(username="******", first_name="first1", last_name="last1", email="*****@*****.**", password="******") self.calendar1 = Calendar(name="User1" + "_cal", slug="User1") self.calendar1.save() self.calendar1.create_relation(self.User1) # User2: admin of Group1, Meeting1 self.User2 = User.objects.create_user(username="******", first_name="first2", last_name="last2", email="*****@*****.**", password="******") self.calendar2 = Calendar(name="User2" + "_cal", slug="User2") self.calendar2.save() self.calendar2.create_relation(self.User2) # User3, Not in any group or meeting self.User3 = User.objects.create_user(username="******", first_name="first3", last_name="last3", email="*****@*****.**", password="******") self.calendar3 = Calendar(name="User3" + "_cal", slug="User3") self.calendar3.save() self.calendar3.create_relation(self.User3) # User4, In Group1, Meeting1 self.User4 = User.objects.create_user(username="******", first_name="first4", last_name="last3", email="*****@*****.**", password="******") self.calendar4 = Calendar(name="User4" + "_cal", slug="User4") self.calendar4.save() self.calendar4.create_relation(self.User4) self.Group1 = Group.objects.create(name="Group1", admin=self.User2) self.Membership12 = Membership.objects.create(group=self.Group1, member=self.User2) self.Membership14 = Membership.objects.create(group=self.Group1, member=self.User4) self.Meeting1 = Meeting.objects.create( group=self.Group1, title="testtitle", description="testdescription", start_time=self.convert('2017-12-04T00:00:00'), end_time=self.convert('2017-12-05T00:00:00')) self.Event2 = Event.objects.create( start=self.convert('2017-12-04T00:00:00'), end=self.convert('2017-12-05T00:00:00'), title=self.Meeting1.title, description=self.Meeting1.description, creator=self.User2, calendar_id=self.calendar2.id) self.Event4 = Event.objects.create( start=self.convert('2017-12-04T00:00:00'), end=self.convert('2017-12-05T00:00:00'), title=self.Meeting1.title, description=self.Meeting1.description, creator=self.User4, calendar_id=self.calendar4.id) self.MeetingEventRelationship12 = MeetingEventRelationship.objects.create \ (meeting=self.Meeting1, event=self.Event2) self.MeetingEventRelationship14 = MeetingEventRelationship.objects.create \ (meeting=self.Meeting1, event=self.Event4) def test_send_message(self): login = self.client.login(username="******", password="******") message = "test send message\n" post1 = {"username": self.User4.username} status = messageHandler.send_message(self.User2, self.User4, message) response = self.client.post(reverse("view_notification"), post1) data = response.content.decode('utf-8') self.assertTrue("test send message" in data) def test_create_and_view_a_group(self): login = self.client.login(username="******", password="******") post1 = {"name": "newgroup"} post2 = {"username": "******"} post3 = {"name": "Group1"} response = self.client.post(reverse("creategroup"), post3) group = Group.objects.filter(name="Group1") self.assertEqual(len(group), 1) response = self.client.post(reverse("creategroup"), post1) group = Group.objects.filter(name="newgroup") self.assertEqual(len(group), 1) #test redirect to home data = response.content.decode("utf-8") self.assertRedirects(response, reverse('home')) response = self.client.get(reverse("viewallgroups")) data = response.content.decode('utf-8') self.assertTrue("My Group" in data) response = self.client.post(reverse("viewadmingroups"), post2) data = json.loads(response.content.decode('utf-8')) #test newly created group is shown self.assertEqual(data["admin"], ["newgroup"]) self.assertEqual(data["member"], []) # notice! Haven't tested the meeting shown def test_delete_group(self): login = self.client.login(username="******", password="******") post1 = { "groupid": "Group1", "operationuser": "******" } # User1 doesn't have authorization post2 = { "groupid": "Group1", "operationuser": "******" } # User2 has authorization post3 = {"username": "******"} response = self.client.post(reverse("deletegroup"), post1) groups = Group.objects.filter(name="Group1") membership = Membership.objects.filter(group="Group1") meetingMembership = MeetingEventRelationship.objects.filter( meeting=self.Meeting1) self.assertEqual(len(groups), 1) self.assertEqual(len(membership), 2) self.assertEqual(len(meetingMembership), 2) response = self.client.post(reverse("deletegroup"), post2) groups = Group.objects.filter(name="Group1") membership = Membership.objects.filter(group=self.Group1) meeting = Meeting.objects.filter(group=self.Group1) meetingMembership = MeetingEventRelationship.objects.filter( meeting=self.Meeting1) self.assertEqual(len(groups), 0) self.assertEqual(len(membership), 0) self.assertEqual(len(meeting), 0) self.assertEqual(len(meetingMembership), 0) # user4 could view the notification of group delete response = self.client.post(reverse("view_notification"), post3) data = response.content.decode('utf-8') self.assertTrue("Group1 is removed" in data) self.assertTrue("You are removed from group Group1" in data) self.assertTrue("You are removed from meeting testtitle" in data) def test_add_member_and_accept_or_reject_group_invitation(self): login = self.client.login(username="******", password="******") post1 = { "group_name": "Group1", "memberid": "User1", "group_admin": "User2", "messages": "hello" } post2 = { "group_name": "Group1", "memberid": "User10", "group_admin": "User2", "messages": "hello" } post3 = { "group_name": "Group1", "memberid": "User3", "group_admin": "User1", "messages": "hello" } post4 = {"group_name": "Group1", "username": "******"} response = self.client.post(reverse("addnewmember"), post1) data = json.loads(response.content.decode('utf-8')) self.assertEqual(data["valid"], "true") response = self.client.post(reverse("addnewmember"), post2) data = json.loads(response.content.decode('utf-8')) self.assertEqual(data["valid"], "false-nomember") response = self.client.post(reverse("addnewmember"), post3) data = json.loads(response.content.decode('utf-8')) self.assertEqual(data["valid"], "false-noright") # the invitation of User1 exists invitation = GroupInvitation.objects.filter(recipient=self.User1) self.assertEqual(len(invitation), 1) self.assertEqual(invitation[0].status, 'NO') # reject the group invitation response = self.client.post(reverse("reject_group"), post4) membership = Membership.objects.filter(group="Group1", member=self.User1) invitation = GroupInvitation.objects.filter(recipient=self.User1) self.assertEqual(len(membership), 0) self.assertEqual(invitation[0].status, 'RJ') # accept the group invitation response = self.client.post(reverse("accept"), post4) membership = Membership.objects.filter(group="Group1", member=self.User1) self.assertEqual(len(membership), 1) #accept invitation from group for the first time data = json.loads(response.content.decode('utf-8')) self.assertEqual(data["valid"], "true") self.assertEqual(invitation[0].status, 'AC') response = self.client.post(reverse("accept"), post4) membership = Membership.objects.filter(group="Group1", member=self.User1) self.assertEqual(len(membership), 1) #accept invitation from group for the second time data = json.loads(response.content.decode('utf-8')) self.assertEqual(data["valid"], "false") def test_view_one_group(self): login = self.client.login(username="******", password="******") Membership.objects.create(group=self.Group1, member=self.User1) post1 = {"group_name": "Group1"} response = self.client.post(reverse("showgroup"), post1) data = json.loads(response.content.decode("utf-8")) self.assertEqual(data["admin"], "User2") self.assertTrue("User1" in data["member"]) self.assertTrue("User2" in data["member"]) def test_delete_member_from_group(self): login = self.client.login(username="******", password="******") Membership.objects.create(group=self.Group1, member=self.User1) # delete member in the group(attend a meeting) post1 = { "group_name": "Group1", "memberid": "User4", "operationuser": "******" } # admin should not be able to delete himself/herself post2 = { "group_name": "Group1", "memberid": "User2", "operationuser": "******" } post3 = { "group_name": "Group1", "memberid": "User3", "operationuser": "******" } post4 = {"username": "******"} response = self.client.post(reverse("deletemember"), post1) data = json.loads(response.content.decode("utf-8")) membership = Membership.objects.filter(member=self.User4) meetingmembership = MeetingEventRelationship.objects.filter( meeting=self.Meeting1) self.assertEqual(len(meetingmembership), 1) self.assertEqual(len(membership), 0) self.assertEqual(data["valid"], "true") response = self.client.post(reverse("view_notification"), post4) data = response.content.decode("utf-8") self.assertTrue("You are removed from group Group1" in data) self.assertTrue("You are removed from meeting testtitle" in data) response = self.client.post(reverse("deletemember"), post2) data = json.loads(response.content.decode("utf-8")) self.assertEqual(data["valid"], "false-self") response = self.client.post(reverse("deletemember"), post3) data = json.loads(response.content.decode("utf-8")) self.assertEqual(data["valid"], "false-noright") def test_create_and_view_meeting_and_invitation(self): login = self.client.login(username="******", password="******") post1 = { "group_name": "Group1", "title": "title2", "description": "description2", "start_time": "2017-08-15", "end_time": "2017-08-16" } post2 = { "group_name": "Group1", "title": "title2", "description": "description2", "end_time": "2017-08-16" } post3 = {"group_name": "Group1"} post4 = {"username": "******"} # notice! haven't tested time empty response = self.client.post(reverse("add_meeting"), post1) meeting = Meeting.objects.filter(group='Group1') mer = MeetingEventRelationship.objects.filter(meeting=meeting[1]) self.assertEqual(len(meeting), 2) self.assertEqual(len(mer), 1) data = json.loads(response.content.decode("utf-8")) self.assertEqual(data["valid"], "true") # test view meetings response = self.client.post(reverse("show_meetings"), post3) data = response.content.decode("utf-8") self.assertTrue("title2" in data) self.assertTrue("description2" in data) self.assertTrue("User2" in data) # test view invitation response = self.client.post(reverse("view_meetinginvitation"), post4) data = response.content.decode("utf-8") self.assertTrue("title2" in data) self.assertTrue("description2" in data) def test_handle_meeting_invitation(self): login = self.client.login(username="******", password="******") post1 = { "group_name": "Group1", "title": "title2", "description": "description2", "start_time": "2017-08-15T00:00:00", "end_time": "2017-08-16T00:00:00" } post2 = {"username": "******"} meeting_info = [] meeting_info.append("title2") meeting_info.append("description2") meeting_info.append("Group1") meeting_info.append("2017-08-15T00:00:00") meeting_info.append("2017-08-16T00:00:00") meeting_info_rej = [] meeting_info_rej.append("title2") meeting_info_rej.append("Group1") #first add a meeting to send invitation response = self.client.post(reverse("add_meeting"), post1) response = self.client.post(reverse("view_meetinginvitation"), post2) data = response.content.decode("utf-8") data = json.loads(data) # get the meeting id meetingid = data[0]['meeting']['id'] meeting_info.append(meetingid) meeting_info_rej.append(meetingid) post3 = {"meeting_info[]": meeting_info, "username": "******"} post4 = {"meeting_info[]": meeting_info_rej, "username": "******"} # reject the meeting response = self.client.post(reverse("reject_meeting"), post4) meeting = Meeting.objects.filter(id=meetingid) self.assertEqual(len(meeting), 1) mer = MeetingEventRelationship.objects.filter(meeting=meeting) self.assertEqual(len(mer), 1) # accept the meeting response = self.client.post(reverse("accept_meeting"), post3) meeting = Meeting.objects.filter(id=meetingid) self.assertEqual(len(meeting), 1) mer = MeetingEventRelationship.objects.filter(meeting=meeting[0]) self.assertEqual(len(mer), 2) data = response.content.decode("utf-8") data = json.loads(data) self.assertEqual(data['valid'], 'true') # accept the meeting twice response = self.client.post(reverse("accept_meeting"), post3) meeting = Meeting.objects.filter(id=meetingid) mer = MeetingEventRelationship.objects.filter(meeting=meeting[0]) self.assertEqual(len(mer), 2) data = response.content.decode("utf-8") data = json.loads(data) self.assertEqual(data['valid'], 'true') def test_change_meeting(self): login = self.client.login(username="******", password="******") meetingid = self.Meeting1.id post1 = { "id": meetingid, "group_name": "Group1", 'changed_title': "changed_t", "changed_description": "changed_d", "changed_start_time": "2017-08-21T00:00:00", "changed_end_time": "2017-08-21T01:00:00" } post2 = {"username": "******"} response = self.client.post(reverse("change_meeting"), post1) meetings = Meeting.objects.all() self.assertEqual(len(meetings), 1) meeting = Meeting.objects.filter(id=self.Meeting1.id) mer = MeetingEventRelationship.objects.filter(meeting=self.Meeting1) self.assertEqual(len(mer), 0) self.assertEqual(len(meeting), 0) # old meeting is deleted response = self.client.post(reverse("view_notification"), post2) data = response.content.decode("utf-8") self.assertTrue("Meeting testtitle is deleted" in data) # new meeting sends invitation to the member response = self.client.post(reverse("view_meetinginvitation"), post2) data = response.content.decode("utf-8") self.assertTrue("changed_t" in data) def test_delete_meeting(self): login = self.client.login(username="******", password="******") meetingid = self.Meeting1.id post1 = {"id": meetingid} response = self.client.post(reverse("delete_meeting"), post1) meeting = Meeting.objects.filter(id=self.Meeting1.id) self.assertEqual(len(meeting), 0) mer = MeetingEventRelationship.objects.filter(meeting=self.Meeting1) self.assertEqual(len(mer), 0) def test_find_time(self): login = self.client.login(username="******", password="******") post1 = { "start_time": "2017-08-15", "end_time": "2017-08-16", "group_name": "Group1", "timezone": "" } post2 = { "start_time": "2017-08-15", "end_time": "2017-08-16", "group_name": "Group1", "timezone": "UTC" } # start_time: 2017-08-15 # end_time: 2017-12-07 post3 = { "start_time": "1502755200", "end_time": "1512604800", "group_name": "Group1", "timezone": "" } testEvent = Event.objects.create( start=self.convert('2017-08-15T15:00:00'), end=self.convert('2017-08-15T17:00:00'), title='testtitle', description='testdescription', creator=self.User2, calendar_id=self.calendar2.id) response = self.client.post(reverse("find_time"), post1) data = response.content.decode("utf-8") data = json.loads(data) self.assertEqual("2017-08-15T00:00:00+00:00", data['slots'][0][0]) self.assertEqual("2017-08-15T15:00:00+00:00", data['slots'][0][1]) self.assertEqual("2017-08-15T17:00:00+00:00", data['slots'][1][0]) self.assertEqual("2017-08-16T00:00:00+00:00", data['slots'][1][1]) response = self.client.post(reverse("find_time"), post2) data = response.content.decode("utf-8") data = json.loads(data) self.assertEqual("2017-08-15T00:00:00+00:00", data['slots'][0][0]) self.assertEqual("2017-08-15T15:00:00+00:00", data['slots'][0][1]) self.assertEqual("2017-08-15T17:00:00+00:00", data['slots'][1][0]) self.assertEqual("2017-08-16T00:00:00+00:00", data['slots'][1][1]) response = self.client.post(reverse("find_time"), post3) data = response.content.decode("utf-8") data = json.loads(data) self.assertEqual("2017-08-15T00:00:00+00:00", data['slots'][0][0]) self.assertEqual("2017-08-15T15:00:00+00:00", data['slots'][0][1]) self.assertEqual("2017-08-15T17:00:00+00:00", data['slots'][1][0]) self.assertEqual("2017-12-04T00:00:00+00:00", data['slots'][1][1]) self.assertEqual("2017-12-05T00:00:00+00:00", data['slots'][2][0]) self.assertEqual("2017-12-07T00:00:00+00:00", data['slots'][2][1]) def test_api_group(self): login = self.client.login(username="******", password="******") post1 = { "start": "2017-03-15", "end": "2017-09-16", "group_name": "Group1", "timezone": "" } testEvent1 = Event.objects.create( start=self.convert('2017-08-15T15:00:00'), end=self.convert('2017-08-15T17:00:00'), title='testtitle1', description='testdescription1', creator=self.User4, calendar_id=self.calendar4.id) testEvent2 = Event.objects.create( start=self.convert('2017-08-15T05:00:00'), end=self.convert('2017-08-15T07:00:00'), title='testtitle2', description='testdescription2', creator=self.User2, calendar_id=self.calendar2.id) response = self.client.post(reverse("api_group"), post1) data = response.content.decode("utf-8") data = json.loads(data) self.assertEqual("2017-08-15T05:00:00Z", data[0]['start']) self.assertEqual("2017-08-15T07:00:00Z", data[0]['end']) self.assertEqual("2017-08-15T15:00:00Z", data[1]['start']) self.assertEqual("2017-08-15T17:00:00Z", data[1]['end'])