import json
from django.test import Client
from django.urls import reverse
from rest_framework import status
from authentication.tests import UserTestMixin, SignatureAuthClient, ToolshedTestCase
from toolshed.tests import WorkflowTestMixin
from toolshed.models import WorkflowInstance

anonymous_client = Client()
client = SignatureAuthClient()


class WorkflowInstanceApiTestCase(UserTestMixin, WorkflowTestMixin, ToolshedTestCase):
    """Comprehensive test cases for the Workflow API"""

    def setUp(self):
        super().setUp()
        self.prepare_users()
        self.prepare_workflows()


    def test_get_workflow_instances(self):
        reply = client.get('/api/workflows/', self.f['local_user1'])
        self.assertEqual(reply.status_code, status.HTTP_200_OK)
        self.assertEqual(len(reply.data), 3)
        self.assertEqual(reply.data[0]['name'], 'workflow1')
        self.assertEqual(reply.data[1]['name'], 'workflow1')
        self.assertEqual(reply.data[2]['name'], 'workflow1')
        self.assertEqual(reply.data[0]['state'], 'initial')
        self.assertEqual(reply.data[1]['state'], 'upload')
        self.assertEqual(reply.data[2]['state'], 'describe')
        self.assertEqual(json.loads(reply.data[0]['payload']), {})
        self.assertEqual(json.loads(reply.data[1]['payload']), {'files': ['ef35c4a9b2d1c4f1a3e6f7d8c9b0a1b2']})
        self.assertEqual(json.loads(reply.data[2]['payload']), {'files': ['ef35c4a9b2d1c4f1a3e6f7d8c9b0a1b2',
                                                              'a1b2c3d4e5f60718293a4b5c6d7e8f90',
                                                                'b1c2d3e4f5a60718293b4c5d6e7f8090'],
                                                    'descriptions': ['file 1 description']})



    def test_get_workflow_instances_user2(self):
        reply = client.get('/api/workflows/', self.f['local_user2'])
        self.assertEqual(reply.status_code, status.HTTP_200_OK)
        self.assertEqual(len(reply.data), 1)
        self.assertEqual(reply.data[0]['name'], 'workflow2')
        self.assertEqual(reply.data[0]['state'], 'initial')
        self.assertEqual(json.loads(reply.data[0]['payload']), {})

