Ejemplo n.º 1
0
    def test_searching_by_username_returns_correct_items(
            self, client, logged_in_user):
        team = TeamFactory(slug='mine')
        TeamMembershipFactory(team=team, user=logged_in_user)
        person1 = PersonFactory(team=team, name='Jaska Jokunen')
        person2 = PersonFactory(team=team, name='Pekka Puupää')

        tab_item = TabItemFactory(team=team,
                                  adder=logged_in_user,
                                  person=person1,
                                  name='Oldest',
                                  added_at=datetime.datetime.now() -
                                  datetime.timedelta(days=1))
        TabItemFactory(team=team,
                       adder=logged_in_user,
                       person=person2,
                       name='Oldest',
                       added_at=datetime.datetime.now() -
                       datetime.timedelta(days=1))

        response = client.get(
            url_for('api.tab_items', team_slug='mine', search='joku'))
        assert response.status_code == 200
        expected_data = {
            'meta': {
                'page': 0,
                'per_page': 50,
                'count': 1
            },
            'data': [{
                'adder': {
                    'name': 'Pekka Puupää',
                    'id': 1
                },
                'price': '2.00',
                'total': '2.00',
                'person': {
                    'name': 'Jaska Jokunen',
                    'id': 1
                },
                'name': 'Oldest',
                'added_at': tab_item.added_at.isoformat(),
                'id': tab_item.id,
                'amount': 1,
                'can_be_deleted': False
            }]
        }
        assert response.json == expected_data
Ejemplo n.º 2
0
    def test_doesnt_return_persons_from_wrong_teams(
        self, client, logged_in_user, team
    ):
        other_users_team = TeamFactory()
        TeamMembershipFactory(
            team=other_users_team,
            user=logged_in_user
        )
        other_team = TeamFactory()

        PersonFactory(team=other_team)
        PersonFactory(team=other_users_team)

        response = client.get(url_for('api.persons', team_slug=team.slug))
        assert response.status_code == 200
        assert response.json == []
Ejemplo n.º 3
0
 def test_deleting_tab_item(self, logged_in_user, client, team):
     tab_item = TabItemFactory(team=team,
                               person=PersonFactory(team=team),
                               adder=logged_in_user)
     assert TabItem.query.count() == 1
     response = client.delete(
         url_for('api.tab_items_delete',
                 team_slug=team.slug,
                 tab_item_id=tab_item.id))
     assert response.status_code == 200
     assert TabItem.query.count() == 0
Ejemplo n.º 4
0
 def test_returns_correct_data(self, client, logged_in_user, team):
     PersonFactory(
         team=team,
         name='Pekka Unkuri'
     )
     person = PersonFactory(
         team=team,
         name='Mikko Puupää',
         disabled=True
     )
     TabItemFactory(
         person=person,
         adder=logged_in_user,
         team=team
     )
     TabItemFactory(
         person=person,
         adder=logged_in_user,
         team=team,
         price=10,
         amount=10
     )
     response = client.get(url_for('api.persons', team_slug=team.slug))
     assert response.status_code == 200
     expected_data = [
         {
             'total_depth': '102.00',
             'team_id': 1,
             'id': 2,
             'disabled': True,
             'name': 'Mikko Puupää'
         },
         {
             'total_depth': '0.00',
             'team_id': 1,
             'id': 1,
             'disabled': False,
             'name': 'Pekka Unkuri'
         },
     ]
     assert response.json == expected_data
Ejemplo n.º 5
0
 def test_cant_edit(self, logged_in_user, client, team):
     person = PersonFactory(
         name='Dan Drinker',
         disabled=False,
         team=team
     )
     response = client.put(
         url_for('api.modify_person', team_slug=team.slug, person_id=person.id),
         data=json.dumps({'disabled': True}),
         content_type='application/json'
     )
     assert response.status_code == 403
Ejemplo n.º 6
0
 def test_validates_person_id_form_wrong_team(self, client, logged_in_user,
                                              data):
     data['person_id'] = PersonFactory(team=TeamFactory()).id
     response = client.post(url_for('api.tab_items', team_slug='my-team'),
                            data=json.dumps(data),
                            content_type='application/json')
     assert response.status_code == 400
     assert response.json == {
         'errors': {
             'person_id': ['Person not found for this id.']
         }
     }
Ejemplo n.º 7
0
    def test_doesnt_return_extra_data(self, client, logged_in_user):
        team = TeamFactory(slug='mine')
        TeamMembershipFactory(team=team, user=logged_in_user)
        person = PersonFactory(team=team, name='Jaska Jokunen')

        tab_item = TabItemFactory(team=team,
                                  adder=logged_in_user,
                                  person=person)

        new_team = TeamFactory()
        new_user = UserFactory()
        new_person = PersonFactory(team=new_team)
        TabItemFactory(team=new_team, adder=new_user, person=new_person)

        response = client.get(url_for('api.tab_items', team_slug='mine'))
        assert response.status_code == 200
        assert response.json == {
            'meta': {
                'page': 0,
                'per_page': 50,
                'count': 1
            },
            'data': [{
                'adder': {
                    'name': 'Pekka Puupää',
                    'id': 1
                },
                'price': '2.00',
                'total': '2.00',
                'person': {
                    'name': 'Jaska Jokunen',
                    'id': 1
                },
                'name': 'Beer',
                'added_at': tab_item.added_at.isoformat(),
                'id': 1,
                'amount': 1,
                'can_be_deleted': True
            }]
        }
Ejemplo n.º 8
0
 def test_user_from_other_team_cant_delete(self, logged_in_user, client,
                                           team):
     other_team = TeamFactory()
     tab_item = TabItemFactory(
         team=other_team,
         person=PersonFactory(team=other_team),
         adder=UserFactory(
             memberships=[TeamMembershipFactory.build(team=other_team)]),
         added_at=datetime.datetime.now())
     assert TabItem.query.count() == 1
     response = client.delete(
         url_for('api.tab_items_delete',
                 team_slug=other_team.slug,
                 tab_item_id=tab_item.id))
     assert response.status_code == 404
     assert TabItem.query.count() == 1
Ejemplo n.º 9
0
    def test_pagination_returns_at_most_50_items(self, client, logged_in_user):
        team = TeamFactory(slug='mine')
        TeamMembershipFactory(team=team, user=logged_in_user)
        person = PersonFactory(team=team, name='Jaska Jokunen')

        for i in range(55):
            TabItemFactory(team=team, adder=logged_in_user, person=person)

        response = client.get(url_for('api.tab_items', team_slug='mine'))
        assert response.status_code == 200
        assert len(response.json['data']) == 50
        assert response.json['meta'] == {
            'page': 0,
            'per_page': 50,
            'count': 55
        }
Ejemplo n.º 10
0
 def test_cant_delete_older_than_60_min_tab_item(self, logged_in_user,
                                                 client, team):
     tab_item = TabItemFactory(team=team,
                               person=PersonFactory(team=team),
                               adder=logged_in_user,
                               added_at=datetime.datetime.now() -
                               datetime.timedelta(minutes=61))
     assert TabItem.query.count() == 1
     response = client.delete(
         url_for('api.tab_items_delete',
                 team_slug=team.slug,
                 tab_item_id=tab_item.id))
     assert response.status_code == 400
     assert TabItem.query.count() == 1
     assert response.json == {
         'errors': {
             'error': ['Items older than 1 hour cant be deleted.']
         }
     }
Ejemplo n.º 11
0
    def test_pagination_with_params_returns_correct_items(
            self, client, logged_in_user):
        team = TeamFactory(slug='mine')
        TeamMembershipFactory(team=team, user=logged_in_user)
        person = PersonFactory(team=team, name='Jaska Jokunen')

        for i in range(50):
            TabItemFactory(team=team, adder=logged_in_user, person=person)
        tab_item = TabItemFactory(team=team,
                                  adder=logged_in_user,
                                  person=person,
                                  name='Oldest',
                                  added_at=datetime.datetime.now() -
                                  datetime.timedelta(days=1))

        response = client.get(
            url_for('api.tab_items', team_slug='mine', page=1))
        assert response.status_code == 200
        assert response.json == {
            'meta': {
                'page': 1,
                'per_page': 50,
                'count': 51
            },
            'data': [{
                'adder': {
                    'name': 'Pekka Puupää',
                    'id': 1
                },
                'price': '2.00',
                'total': '2.00',
                'person': {
                    'name': 'Jaska Jokunen',
                    'id': 1
                },
                'name': 'Oldest',
                'added_at': tab_item.added_at.isoformat(),
                'id': tab_item.id,
                'amount': 1,
                'can_be_deleted': False
            }]
        }
Ejemplo n.º 12
0
 def person(self, team):
     return PersonFactory(team=team)