import unittest

from flask import json

from api.v1.business.user import create_user
from tests import RestAPIUnitTest


class UserCodesTestCase(RestAPIUnitTest):

    example_user = dict(
        {
            'login': 'demo',
            'password': 'demo'
        }
    )

    bad_user = dict(
        {
            'name': 'demo',
            'password': 'demo'
        }
    )

    # GET /users/
    def test_get_200_code(self):
        result = self.app.get('/v1/users/', follow_redirects=False)
        assert result.status_code == 200

    # POST /users/
    def test_post_201_code(self):
        result = self.app.post('/v1/users/', follow_redirects=False, data=json.dumps(self.example_user),
                               content_type='application/json')
        assert result.status_code == 201

    def test_post_400_code(self):
        result = self.app.post('/v1/users/', follow_redirects=False, data=json.dumps(self.bad_user),
                               content_type='application/json')
        assert result.status_code == 400

    # DELETE /users/{id}
    def test_delete_204_code(self):
        id = None
        with self.app_db.app_context():
            id = create_user(self.example_user)
        assert id is not None
        result = self.app.delete('/v1/users/{}'.format(id), follow_redirects=False)
        assert result.status_code == 204

    # GET /users/{id}
    def test_details_get_200_code(self):
        id = None
        with self.app_db.app_context():
            id = create_user(self.example_user)
        assert id is not None
        result = self.app.get('/v1/users/{}'.format(id), follow_redirects=False)
        assert result.status_code == 200

    # POST /users/{id}
    def test_post_get_204_code(self):
        id = None
        with self.app_db.app_context():
            id = create_user(self.example_user)
        assert id is not None
        result = self.app.put('/v1/users/{}'.format(id), follow_redirects=False, data=json.dumps(self.example_user),
                              content_type='application/json')
        assert result.status_code == 204

if __name__ == '__main__':
    unittest.main()
