from __future__ import print_function, unicode_literals

from gratipay.testing import Harness
from gratipay.models.participant import Participant

workflow = ['pending-application', 'pending-review', 'rejected', 'pending-payout', 'completed']


class Tests(Harness):
    def setUp(self):
        super(Harness)
        self.make_participant('alice', claimed_time='now')
        self.make_participant('admin', claimed_time='now', is_admin=True)

    def hit(self, new_status, auth_as='admin'):
        return self.client.PxST(
            "/~alice/payout-status",
            {'to': new_status},
            auth_as=auth_as
        )

    def test_admin_can_change_status(self):
        for status in workflow:
            response = self.hit(status)
            assert response.code == 302
            assert Participant.from_username('alice').status_of_1_0_payout == status

    def test_user_cant_change_status(self):
        response = self.hit('pending-payout', auth_as='alice')
        assert response.code == 401
        assert Participant.from_username('alice').status_of_1_0_payout == 'completed'

    def test_invalid_is_400(self):
        response = self.hit('invalid_status')
        assert response.code == 400

@@ -1216,8 +1216,631 @@
  == 400%0A
+%0A    def test_can_update_status_via_trigger_on_participant_balance(self):%0A        self.db.run(%22UPDATE participants %22%0A                    %22SET balance=10, status_of_1_0_payout='pending-application' %22%0A                    %22WHERE username='alice'%22)%0A        alice = Participant.from_username('alice')%0A        assert alice.balance == 10%0A        assert alice.status_of_1_0_payout == 'pending-application';%0A%0A        self.db.run(%22UPDATE participants SET balance=0 WHERE username='alice'%22)%0A        alice = Participant.from_username('alice')%0A        assert alice.balance == 0%0A        assert alice.status_of_1_0_payout == 'completed';%0A

Add a test for the status update trigger