def test_delete_entity(self):
        """testing if delete_entity() method is working properly
        """
        from stalker import db, User, Vacation
        user1 = User(name='Test User 1',
                     login='******',
                     email='*****@*****.**',
                     password='******')
        db.DBSession.add(user1)

        import datetime
        start = datetime.datetime(2016, 4, 22, 10)
        end = datetime.datetime(2016, 4, 22, 16)
        vac1 = Vacation(user=user1, start=start, end=end)

        db.DBSession.commit()

        vac1 = Vacation.query.filter(Vacation.name == vac1.name).first()

        from stalker_pyramid.testing import DummyRequest
        request = DummyRequest()
        request.matchdict['id'] = vac1.id

        vacation_views = vacation.VacationViews(request)
        vacation_views.delete_entity()

        vac = Vacation.query.filter(Vacation.name == vac1.name).all()
        self.assertEqual(vac, [])
    def test_update_entity_change_end_with_post(self):
        """testing POST: /api/vacations/{id}?end={value} is working properly
        """
        from stalker import db, User, Vacation
        user1 = User(name='Test User 1',
                     login='******',
                     email='*****@*****.**',
                     password='******')
        db.DBSession.add(user1)

        import datetime
        start = datetime.datetime(2016, 4, 22, 10)
        end = datetime.datetime(2016, 4, 22, 16)
        new_end = datetime.datetime(2016, 4, 22, 17)
        vac1 = Vacation(user=user1, start=start, end=end)
        db.DBSession.commit()

        user1 = User.query.filter(User.login == user1.login).first()
        vac1 = Vacation.query.filter(Vacation.name == vac1.name).first()

        from stalker_pyramid.views import EntityViewBase
        self.admin_login()
        self.test_app.patch(
            '/api/vacations/%s' % vac1.id,
            params={'end': EntityViewBase.milliseconds_since_epoch(new_end)},
            status=200)

        vac = Vacation.query.filter(Vacation.name == vac1.name).first()
        self.assertEqual(vac.start, start)
        self.assertEqual(vac.end, new_end)
        self.assertEqual(vac.user, user1)
    def test_update_entity_change_user(self):
        """testing if update_entity() method is working properly for changing
        user attribute
        """
        from stalker import db, User, Vacation
        user1 = User(name='Test User 1',
                     login='******',
                     email='*****@*****.**',
                     password='******')
        db.DBSession.add(user1)

        user2 = User(name='Test User 2',
                     login='******',
                     email='*****@*****.**',
                     password='******')
        db.DBSession.add(user2)

        import datetime
        start = datetime.datetime(2016, 4, 22, 10)
        end = datetime.datetime(2016, 4, 22, 16)
        vac1 = Vacation(user=user1, start=start, end=end)

        db.DBSession.commit()

        user2 = User.query.filter(User.login == user2.login).first()
        vac1 = Vacation.query.filter(Vacation.name == vac1.name).first()
        # also update updated_by_id attribute

        from stalker_pyramid.testing import DummyRequest, DummyMultiDict
        request = DummyRequest()
        request.matchdict['id'] = vac1.id

        request.params = DummyMultiDict()

        # change user
        request.params['user_id'] = user2.id
        request.params['updated_by_id'] = user2.id

        self.patch_logged_in_user(request)
        vacation_views = vacation.VacationViews(request)
        vacation_views.update_entity()

        vac = Vacation.query.filter(Vacation.name == vac1.name).first()
        self.assertEqual(vac.start, start)
        self.assertEqual(vac.end, end)
        self.assertEqual(vac.user, user2)
        self.assertEqual(vac.updated_by, user2)
    def test_update_entity_change_end(self):
        """testing if update_entity() method is working properly for changing
        end attribute
        """
        from stalker import db, User, Vacation
        user1 = User(name='Test User 1',
                     login='******',
                     email='*****@*****.**',
                     password='******')
        db.DBSession.add(user1)

        import datetime
        start = datetime.datetime(2016, 4, 22, 10)
        end = datetime.datetime(2016, 4, 22, 16)
        new_end = datetime.datetime(2016, 4, 22, 17)
        vac1 = Vacation(user=user1, start=start, end=end)

        db.DBSession.commit()

        user1 = User.query.filter(User.login == user1.login).first()
        vac1 = Vacation.query.filter(Vacation.name == vac1.name).first()

        from stalker_pyramid.testing import DummyRequest, DummyMultiDict
        request = DummyRequest()
        request.matchdict['id'] = vac1.id

        request.params = DummyMultiDict()

        # change start
        from stalker_pyramid.views import EntityViewBase
        request.params['end'] = \
            EntityViewBase.milliseconds_since_epoch(new_end)

        self.patch_logged_in_user(request)
        vacation_views = vacation.VacationViews(request)
        vacation_views.update_entity()

        vac = Vacation.query.filter(Vacation.name == vac1.name).first()
        self.assertEqual(vac.start, start)
        self.assertEqual(vac.end, new_end)
        self.assertEqual(vac.user, user1)
    def test_update_entity_change_user_with_post(self):
        """testing if POST: /api/vacations/{id}?user_id={value} is working
        properly
        """
        from stalker import db, User, Vacation
        user1 = User(name='Test User 1',
                     login='******',
                     email='*****@*****.**',
                     password='******')
        db.DBSession.add(user1)

        user2 = User(name='Test User 2',
                     login='******',
                     email='*****@*****.**',
                     password='******')
        db.DBSession.add(user2)

        import datetime
        start = datetime.datetime(2016, 4, 22, 10)
        end = datetime.datetime(2016, 4, 22, 16)
        vac1 = Vacation(user=user1, start=start, end=end)

        db.DBSession.commit()

        user2 = User.query.filter(User.login == user2.login).first()
        vac1 = Vacation.query.filter(Vacation.name == vac1.name).first()
        # also update updated_by_id attribute

        response = self.test_app.post('/api/vacations/%s' % vac1.id,
                                      params={
                                          'user_id': user2.id,
                                          'updated_by_id': user2.id
                                      })

        vac = Vacation.query.filter(Vacation.name == vac1.name).first()
        self.assertEqual(vac.start, start)
        self.assertEqual(vac.end, end)
        self.assertEqual(vac.user, user2)
        self.assertEqual(vac.updated_by, user2)
    def test_delete_entity_is_working_properly(self):
        """testing if DELETE: /api/vacations/{id} view is working properly
        """
        from stalker import db, User, Vacation
        user1 = User(name='Test User 1',
                     login='******',
                     email='*****@*****.**',
                     password='******')
        db.DBSession.add(user1)

        import datetime
        start = datetime.datetime(2016, 4, 22, 10)
        end = datetime.datetime(2016, 4, 22, 16)
        vac1 = Vacation(user=user1, start=start, end=end)

        db.DBSession.commit()

        vac1 = Vacation.query.filter(Vacation.name == vac1.name).first()

        self.test_app.delete('/api/vacations/%s' % vac1.id, status=200)

        vac = Vacation.query.filter(Vacation.name == vac1.name).all()
        self.assertEqual(vac, [])
    def test_get_entity_is_working_properly(self):
        """test if GET: /api/vacation/{id} view is working properly
        """
        # login as admin
        self.admin_login()

        # create a vacation instance
        from stalker import db, User, Vacation
        user = User(name='Test User 1',
                    login='******',
                    email='*****@*****.**',
                    password='******')
        db.DBSession.add(user)

        admin = User.query.filter(User.login == 'admin').first()

        import datetime
        vac = Vacation(user=user,
                       start=datetime.datetime(2016, 4, 22, 10, 0),
                       end=datetime.datetime(2016, 4, 24, 10, 0),
                       created_by=admin)
        db.DBSession.add(vac)
        db.DBSession.commit()

        response = self.test_app.get('/api/vacations/%s' % vac.id)
        from stalker_pyramid.views import EntityViewBase
        self.maxDiff = None
        import stalker
        self.assertEqual(
            response.json, {
                'created_by': {
                    'id': admin.id,
                    '$ref': '/api/users/%s' % admin.id,
                    'name': admin.name,
                    'entity_type': 'User'
                },
                'date_created':
                EntityViewBase.milliseconds_since_epoch(vac.date_created),
                'date_updated':
                EntityViewBase.milliseconds_since_epoch(vac.date_created),
                'description':
                '',
                'end':
                EntityViewBase.milliseconds_since_epoch(vac.end),
                'entity_type':
                'Vacation',
                'id':
                vac.id,
                'generic_text':
                '',
                'generic_data': {
                    '$ref': '/api/simple_entities/%s/generic_data' % vac.id,
                    'length': 0
                },
                'name':
                vac.name,
                'stalker_version':
                stalker.__version__,
                'start':
                EntityViewBase.milliseconds_since_epoch(vac.start),
                'thumbnail':
                None,
                'type':
                None,
                'updated_by': {
                    'id': admin.id,
                    '$ref': '/api/users/%s' % admin.id,
                    'name': admin.name,
                    'entity_type': 'User'
                },
                'user': {
                    'id': vac.user_id,
                    '$ref': '/api/users/%s' % vac.user_id,
                    'name': vac.user.name,
                    'entity_type': 'User'
                }
            })
    def test_get_entity_is_working_properly(self):
        """testing if get_entity() method is working properly
        """
        from stalker import db, User, Vacation
        user1 = User(name='Test User 1',
                     login='******',
                     email='*****@*****.**',
                     password='******')
        db.DBSession.add(user1)

        import datetime
        start = datetime.datetime(2016, 4, 22)
        end = datetime.datetime(2016, 4, 23)

        # get admin
        admin = User.query.filter(User.login == 'admin').first()

        vac1 = Vacation(
            user=user1,
            start=start,
            end=end,
            created_by=admin,
        )
        db.DBSession.add(vac1)
        db.DBSession.commit()

        # get the id
        vac1 = Vacation.query.filter(Vacation.name == vac1.name).first()

        from stalker_pyramid.testing import DummyRequest
        request = DummyRequest()
        request.matchdict['id'] = vac1.id

        vacation_views = vacation.VacationViews(request)
        response = vacation_views.get_entity()

        from stalker_pyramid.views import EntityViewBase
        import stalker
        self.maxDiff = None
        self.assertEqual(
            response.json_body, {
                'created_by': {
                    'id': vac1.created_by.id,
                    '$ref': '/api/users/%s' % vac1.created_by.id,
                    'name': vac1.created_by.name,
                    'entity_type': 'User'
                },
                'date_created':
                EntityViewBase.milliseconds_since_epoch(vac1.date_created),
                'date_updated':
                EntityViewBase.milliseconds_since_epoch(vac1.date_updated),
                'description':
                '',
                'end':
                EntityViewBase.milliseconds_since_epoch(vac1.end),
                'entity_type':
                'Vacation',
                'generic_text':
                '',
                'generic_data': {
                    '$ref': '/api/simple_entities/%s/generic_data' % vac1.id,
                    'length': 0
                },
                'id':
                vac1.id,
                'name':
                vac1.name,
                'stalker_version':
                stalker.__version__,
                'start':
                EntityViewBase.milliseconds_since_epoch(vac1.start),
                'thumbnail':
                None,
                'type':
                None,
                'user': {
                    'id': vac1.user.id,
                    '$ref': '/api/users/%s' % vac1.user.id,
                    'name': vac1.user.name,
                    'entity_type': 'User'
                },
                'updated_by': {
                    'id': vac1.updated_by.id,
                    '$ref': '/api/users/%s' % vac1.updated_by.id,
                    'name': vac1.updated_by.name,
                    'entity_type': 'User'
                },
            })
    def test_get_entities_is_working_properly(self):
        """testing if get_entities() method is working properly
        """
        from stalker import db, User, Vacation
        user1 = User(name='Test User 1',
                     login='******',
                     email='*****@*****.**',
                     password='******')
        db.DBSession.add(user1)

        user2 = User(name='Test User 2',
                     login='******',
                     email='*****@*****.**',
                     password='******')
        db.DBSession.add(user2)

        admin = User.query.filter(User.login == 'admin').first()

        import datetime
        vac1 = Vacation(user=user1,
                        start=datetime.datetime(2016, 4, 22),
                        end=datetime.datetime(2016, 4, 23),
                        created_by=admin)
        db.DBSession.add(vac1)

        vac2 = Vacation(user=user1,
                        start=datetime.datetime(2016, 4, 24),
                        end=datetime.datetime(2016, 4, 25),
                        created_by=admin)
        db.DBSession.add(vac2)

        vac3 = Vacation(user=user2,
                        start=datetime.datetime(2016, 4, 10),
                        end=datetime.datetime(2016, 4, 12),
                        created_by=admin)
        db.DBSession.add(vac3)

        vac4 = Vacation(user=user2,
                        start=datetime.datetime(2016, 4, 13),
                        end=datetime.datetime(2016, 4, 20),
                        created_by=admin)
        db.DBSession.add(vac4)

        db.DBSession.commit()

        # get the id
        vac1 = Vacation.query.filter(Vacation.name == vac1.name).first()
        vac2 = Vacation.query.filter(Vacation.name == vac2.name).first()
        vac3 = Vacation.query.filter(Vacation.name == vac3.name).first()
        vac4 = Vacation.query.filter(Vacation.name == vac4.name).first()

        from stalker_pyramid.testing import DummyRequest
        request = DummyRequest()

        vacation_views = vacation.VacationViews(request)
        response = vacation_views.get_entities()

        self.assertEqual(response.json_body, [{
            'id': v.id,
            '$ref': '/api/vacations/%s' % v.id,
            'name': v.name,
            'entity_type': 'Vacation'
        } for v in [vac1, vac2, vac3, vac4]])