def setUp(self):

        self.factory = RequestFactory()

        self.request = self.factory.post('/android/registerReport/')
        self.request.user = AnonymousUser()
        self.reponseView = RegisterReport()

        # inputs
        self.userId = '067e6162-3b6f-4ae2-a171-2470b63dff00'
        self.textMessage = 'this is a comment for testing purpose'
        with open(os.path.join(os.path.dirname(__file__), 'registerReportTestImage.jpg')) as imageFile:
            self.image = base64.b64encode(imageFile.read())
        self.formatImage = 'JPEG'
        self.reportInfo = 'additional info'

        POST = {}
        POST['text'] = self.textMessage
        POST['img'] = self.image
        POST['ext'] = self.formatImage
        POST['userId'] = self.userId
        POST['report_info'] = self.reportInfo

        self.request.POST = POST
class RegisterReportTestCase(TestCase):
    """ test for register report view """

    def setUp(self):

        self.factory = RequestFactory()

        self.request = self.factory.post('/android/registerReport/')
        self.request.user = AnonymousUser()
        self.reponseView = RegisterReport()

        # inputs
        self.userId = '067e6162-3b6f-4ae2-a171-2470b63dff00'
        self.textMessage = 'this is a comment for testing purpose'
        with open(os.path.join(os.path.dirname(__file__), 'registerReportTestImage.jpg')) as imageFile:
            self.image = base64.b64encode(imageFile.read())
        self.formatImage = 'JPEG'
        self.reportInfo = 'additional info'

        POST = {}
        POST['text'] = self.textMessage
        POST['img'] = self.image
        POST['ext'] = self.formatImage
        POST['userId'] = self.userId
        POST['report_info'] = self.reportInfo

        self.request.POST = POST

    def test_send_report_with_all_data(self):
        request = self.request

        response = self.reponseView.post(request)
        jsonResponse = json.loads(response.content)

        self.assertTrue(jsonResponse['valid'])
        self.assertEqual(Report.objects.all().count(), 1)

        # delete image file saved
        report = Report.objects.get(userId=self.userId)
        path = os.path.join(settings.MEDIA_IMAGE, report.imageName)
        os.remove(path)

    def test_send_report_without_text(self):
        request = self.request
        request.POST.pop('text')

        response = self.reponseView.post(request)
        jsonResponse = json.loads(response.content)

        self.assertFalse(jsonResponse['valid'])
        self.assertEqual(Report.objects.all().count(), 0)

    def test_send_report_without_image(self):
        request = self.request
        request.POST.pop('img')

        response = self.reponseView.post(request)
        jsonResponse = json.loads(response.content)

        self.assertTrue(jsonResponse['valid'])
        self.assertEqual(Report.objects.all().count(), 1)

    def test_send_report_without_image_and_without_extension(self):
        request = self.request
        request.POST.pop('img')
        request.POST.pop('ext')

        response = self.reponseView.post(request)
        jsonResponse = json.loads(response.content)

        self.assertTrue(jsonResponse['valid'])
        self.assertEqual(Report.objects.all().count(), 1)

    def test_send_report_with_image_but_without_extension(self):
        request = self.request
        request.POST.pop('ext')

        response = self.reponseView.post(request)
        jsonResponse = json.loads(response.content)

        self.assertFalse(jsonResponse['valid'])
        self.assertEqual(Report.objects.all().count(), 0)

    def test_send_report_with_image_but_with_invalid_extension(self):
        request = self.request
        request.POST['ext'] = 'JPPNG'

        response = self.reponseView.post(request)
        jsonResponse = json.loads(response.content)

        self.assertFalse(jsonResponse['valid'])
        self.assertEqual(Report.objects.all().count(), 0)

    def test_send_report_without_user_id(self):
        request = self.request
        request.POST.pop('userId')

        with transaction.atomic():
            response = self.reponseView.post(request)
        jsonResponse = json.loads(response.content)

        self.assertFalse(jsonResponse['valid'])
        self.assertEqual(Report.objects.all().count(), 0)

    def test_send_report_without_additional_info(self):
        request = self.request
        request.POST.pop('report_info')

        response = self.reponseView.post(request)
        jsonResponse = json.loads(response.content)

        self.assertTrue(jsonResponse['valid'])
        self.assertEqual(Report.objects.all().count(), 1)

        # delete image file saved
        report = Report.objects.get(userId=self.userId)
        path = os.path.join(settings.MEDIA_IMAGE, report.imageName)
        os.remove(path)
Exemplo n.º 3
0
from AndroidRequests.allviews.RegisterEventBusV2 import RegisterEventBusV2
from AndroidRequests.allviews.RegisterEventBusStop import RegisterEventBusStop
from AndroidRequests.allviews.EventsByBus import EventsByBus
from AndroidRequests.allviews.EventsByBusV2 import EventsByBusV2
from AndroidRequests.allviews.RequestEventsToNotified import RequestEventsToNotified
from AndroidRequests.allviews.EventsByBusStop import EventsByBusStop
from AndroidRequests.allviews.BusStopsByService import BusStopsByService
from AndroidRequests.allviews.RegisterReport import RegisterReport
from AndroidRequests.allviews.ServiceRoute import ServiceRoute
from AndroidRequests.allviews.SetDirection import SetDirection
from AndroidRequests.allviews.RequestUUID import RequestUUID

urlpatterns = [
    url(r'^nearbyBuses/(?P<pUserId>[0-9a-z-]+)/(?P<pBusStop>\w+)$',
        views.nearbyBuses),
    url(r'^registerReport$', RegisterReport.as_view()),
    url(
        r'^userPosition/(?P<pUserId>[0-9a-z-]+)/(?P<pLat>[\-+]?[0-9]*\.?[0-9]*)/(?P<pLon>[\-+]?[0-9]*\.?[0-9]*)$',
        views.userPosition),
    url(
        r'^requestToken/(?P<pUserId>[0-9a-z-]+)/(?P<pBusService>[0-9,\w]*)/(?P<pRegistrationPlate>[0-9,\w,-]{6,8})$',
        RequestToken.as_view()),
    url(
        r'^requestToken/v2/(?P<pUserId>[0-9a-z-]+)/(?P<pBusService>[0-9,\w]*)/(?P<pUUID>[0-9a-z-]+)$',
        RequestTokenV2.as_view()),
    url(r'^endRoute/(?P<pToken>[0-9,a-f]{128})$', EndRoute.as_view()),
    url(r'^sendTrajectory$', SendPoses.as_view()),
    # reportEventBus with location
    url(
        r'^reportEventBus/(?P<pUserId>[0-9a-z-]+)/(?P<pBusService>[\w,0-9]*)/(?P<pBusPlate>[\w,0-9,-]*)/(?P<pEventID>.*)/(?P<pLatitud>[\-+]?[0-9]*\.?[0-9]*)/(?P<pLongitud>[\-+]?[0-9]*\.?[0-9]*)/(?P<pConfirmDecline>.*)$',
        RegisterEventBus.as_view()),