"""
Imports

"""
import json
from stackoverflow import settings
from .base_test import BaseTestCase

def register(my_client):
    """Function that holds data for register"""
    resp_register = my_client.post(
        '/api/v2/auth/register',
        data=json.dumps(dict(
            name='<NAME>',
            email='<EMAIL>',
            username='ivy',
            password='<PASSWORD>'
        )),
        content_type='application/json'
    )
    return resp_register

class TestAnswerResource(BaseTestCase):
    """Test answer resource"""
    def test_user_can_post_answer_to_a_question(self):
        """Test user can post an answer"""
        with self.client:
            resp_register = register(self.client)
            resp = self.client.post(
                '/api/v2/questions/newquestion',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    title='Flask Cli',
                    description='How to create cli project in flask?'
                )),
                content_type='application/json'
            )
            resp = self.client.post(
                '/api/v2/questions/1/answers',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    answer='Use click cli'
                )),
                content_type='application/json'
            )
            response_data = json.loads(resp.data.decode())
            self.assertEqual(resp.status_code, 201)

    def test_user_can_upvote_an_answer_to_a_question(self):
        """Test user can upvote an answer"""
        with self.client:
            resp_register = register(self.client)
            resp = self.client.post(
                '/api/v2/questions/newquestion',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    title='Flask Cli',
                    description='How to create cli project in flask?'
                )),
                content_type='application/json'
            )
            resp = self.client.post(
                '/api/v2/questions/1/answers',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    answer='Use click cli'
                )),
                content_type='application/json'
            )
            resp = self.client.patch(
                '/api/v2/questions/1/answers/1/upvote',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                )
            )
            response_data = json.loads(resp.data.decode())
            self.assertEqual(resp.status_code, 200)

    def test_user_can_down_vote_an_answer_to_a_question(self):
        """Test user can downvote an answer"""
        with self.client:
            resp_register = register(self.client)
            resp = self.client.post(
                '/api/v2/questions/newquestion',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    title='Flask Cli',
                    description='How to create cli project in flask?'
                )),
                content_type='application/json'
            )
            resp = self.client.post(
                '/api/v2/questions/1/answers',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    answer='Use click cli'
                )),
                content_type='application/json'
            )
            resp = self.client.patch(
                '/api/v2/questions/1/answers/1/downvote',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                )
            )
            response_data = json.loads(resp.data.decode())
            self.assertEqual(resp.status_code, 200)

    def test_user_can_accept_an_answer_to_their_question(self):
        """test user can accept an answer"""
        with self.client:
            resp_register = register(self.client)
            resp = self.client.post(
                '/api/v2/questions/newquestion',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    title='Flask Cli',
                    description='How to create cli project in flask?'
                )),
                content_type='application/json'
            )
            resp = self.client.post(
                '/api/v2/questions/1/answers',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    answer='Use click cli'
                )),
                content_type='application/json'
            )
            resp = self.client.patch(
                '/api/v2/questions/1/answers/1/accept',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    accepted=settings.ACCEPT
                )),
                content_type='application/json'
            )
            response_data = json.loads(resp.data.decode())
            self.assertEqual(resp.status_code, 200)

"""
Imports

"""
import json
from stackoverflow import settings
from .base_test import BaseTestCase

def register(my_client):
    """Function that holds data for register"""
    resp_register = my_client.post(
        '/api/v2/auth/register',
        data=json.dumps(dict(
            name='<NAME>',
            email='<EMAIL>',
            username='ivy',
            password='<PASSWORD>'
        )),
        content_type='application/json'
    )
    return resp_register

class TestAnswerResource(BaseTestCase):
    """Test answer resource"""
    def test_user_can_post_answer_to_a_question(self):
        """Test user can post an answer"""
        with self.client:
            resp_register = register(self.client)
            resp = self.client.post(
                '/api/v2/questions/newquestion',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    title='Flask Cli',
                    description='How to create cli project in flask?'
                )),
                content_type='application/json'
            )
            resp = self.client.post(
                '/api/v2/questions/1/answers',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    answer='Use click cli'
                )),
                content_type='application/json'
            )
            response_data = json.loads(resp.data.decode())
            self.assertEqual(resp.status_code, 201)

    def test_user_can_upvote_an_answer_to_a_question(self):
        """Test user can upvote an answer"""
        with self.client:
            resp_register = register(self.client)
            resp = self.client.post(
                '/api/v2/questions/newquestion',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    title='Flask Cli',
                    description='How to create cli project in flask?'
                )),
                content_type='application/json'
            )
            resp = self.client.post(
                '/api/v2/questions/1/answers',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    answer='Use click cli'
                )),
                content_type='application/json'
            )
            resp = self.client.patch(
                '/api/v2/questions/1/answers/1/upvote',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                )
            )
            response_data = json.loads(resp.data.decode())
            self.assertEqual(resp.status_code, 200)

    def test_user_can_down_vote_an_answer_to_a_question(self):
        """Test user can downvote an answer"""
        with self.client:
            resp_register = register(self.client)
            resp = self.client.post(
                '/api/v2/questions/newquestion',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    title='Flask Cli',
                    description='How to create cli project in flask?'
                )),
                content_type='application/json'
            )
            resp = self.client.post(
                '/api/v2/questions/1/answers',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    answer='Use click cli'
                )),
                content_type='application/json'
            )
            resp = self.client.patch(
                '/api/v2/questions/1/answers/1/downvote',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                )
            )
            response_data = json.loads(resp.data.decode())
            self.assertEqual(resp.status_code, 200)

    def test_user_can_accept_an_answer_to_their_question(self):
        """test user can accept an answer"""
        with self.client:
            resp_register = register(self.client)
            resp = self.client.post(
                '/api/v2/questions/newquestion',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    title='Flask Cli',
                    description='How to create cli project in flask?'
                )),
                content_type='application/json'
            )
            resp = self.client.post(
                '/api/v2/questions/1/answers',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    answer='Use click cli'
                )),
                content_type='application/json'
            )
            resp = self.client.patch(
                '/api/v2/questions/1/answers/1/accept',
                headers=dict(
                    Authorization='Bearer ' + json.loads(
                        resp_register.data.decode()
                    )['access_token']
                ),
                data=json.dumps(dict(
                    accepted=settings.ACCEPT
                )),
                content_type='application/json'
            )
            response_data = json.loads(resp.data.decode())
            self.assertEqual(resp.status_code, 200)

Imports Function that holds data for register Test answer resource Test user can post an answer Test user can upvote an answer Test user can downvote an answer test user can accept an answer