示例#1
0
    def testStopTimeSlice(self):
        for pk in range(2,7):
            user = User.objects.get(pk=pk)
            for pk in range(1,13):
                slip = Slip.objects.get(pk=pk)
                # Login
                response = self.client.post('/accounts/login/',
                                            {'username': user.username, 'password': '******'})

                # Get slip
                response = self.client.get('/slip/%i/' % slip.pk)

                # Send the request to start a new timeslip
                # first we'll create a time to send, that we can use to compare later
                timeSetBegin = datetime.utcnow()
                begin = '%s, %s, %s, %s, %s, %s, %s' % (timeSetBegin.year, timeSetBegin.month, timeSetBegin.day, timeSetBegin.hour, timeSetBegin.minute, timeSetBegin.second, timeSetBegin.microsecond)
                response = self.client.post('/slip/%i/start/' % slip.pk,
                                           {'begin': begin})

                # Now stopping the timeslip with a new timeSet
                timeSetEnd = datetime.utcnow()
                end = '%s, %s, %s, %s, %s, %s, %s' % (timeSetEnd.year, timeSetEnd.month, timeSetEnd.day, timeSetEnd.hour, timeSetEnd.minute, timeSetEnd.second, timeSetEnd.microsecond)
                response = self.client.post('/slip/%i/stop/' % slip.pk,
                                           {'end': end})
                if user == slip.user:
                    self.failUnlessEqual(response.status_code, 200)
                else:
                    self.failUnlessEqual(response.status_code, 403)

                # Lets see if we have a created timeslice with the correct end time
                # we do that, by try, exepting that is does not exist.
                try:
                    time_slice = TimeSlice.objects.get(begin = timeSetBegin, duration = delta_to_seconds(timeSetEnd - timeSetBegin))
                    if user != slip.user:
                        self.fail('Unauthorized user stopped timeslice')
                    else:
                        self.failUnlessEqual(timeSetBegin, time_slice.begin)

                except TimeSlice.DoesNotExist:
                    if user == slip.user:
                        self.fail('Failed to get TimeSlice, TimeSlice has not been stopped with correct end time')