from contextlib import contextmanager
from os import environ
from unittest import TestCase


class DownloadTests(TestCase):
    def test_denies_unknown_client(self):
        with self._given_clients('{"client1": "id1"}') as download:
            message, status = download('unknown_client')
            self.assertEqual(status, 403)

    @classmethod
    @contextmanager
    def _given_clients(cls, clients: str):
        environ['LOKOLE_CLIENTS'] = clients
        from opwen_email_server.api import client_read
        yield client_read.download
        del client_read

from contextlib import contextmanager
from unittest import TestCase

from opwen_email_server.api import client_read
from opwen_email_server.services.auth import EnvironmentAuth


class DownloadTests(TestCase):
    def test_denies_unknown_client(self):
        with self.given_clients({'client1': 'id1'}):
            message, status = client_read.download('unknown_client')
            self.assertEqual(status, 403)

    @contextmanager
    def given_clients(self, clients):
        original_clients = client_read.CLIENTS
        client_read.CLIENTS = EnvironmentAuth(clients)
        yield
        client_read.CLIENTS = original_clients

Remove need to set environment variables in test
Remove need to set environment variables in test

  from contextlib import contextmanager
- from os import environ
  from unittest import TestCase
+ 
+ from opwen_email_server.api import client_read
+ from opwen_email_server.services.auth import EnvironmentAuth
  
  
  class DownloadTests(TestCase):
      def test_denies_unknown_client(self):
-         with self._given_clients('{"client1": "id1"}') as download:
+         with self.given_clients({'client1': 'id1'}):
-             message, status = download('unknown_client')
+             message, status = client_read.download('unknown_client')
              self.assertEqual(status, 403)
  
-     @classmethod
      @contextmanager
-     def _given_clients(cls, clients: str):
+     def given_clients(self, clients):
-         environ['LOKOLE_CLIENTS'] = clients
-         from opwen_email_server.api import client_read
-         yield client_read.download
-         del client_read
+         original_clients = client_read.CLIENTS
+         client_read.CLIENTS = EnvironmentAuth(clients)
+         yield
+         client_read.CLIENTS = original_clients
  
Remove need to set environment variables in test
## Code Before:
from contextlib import contextmanager
from os import environ
from unittest import TestCase


class DownloadTests(TestCase):
    def test_denies_unknown_client(self):
        with self._given_clients('{"client1": "id1"}') as download:
            message, status = download('unknown_client')
            self.assertEqual(status, 403)

    @classmethod
    @contextmanager
    def _given_clients(cls, clients: str):
        environ['LOKOLE_CLIENTS'] = clients
        from opwen_email_server.api import client_read
        yield client_read.download
        del client_read

## Instruction:
Remove need to set environment variables in test
## Code After:
from contextlib import contextmanager
from unittest import TestCase

from opwen_email_server.api import client_read
from opwen_email_server.services.auth import EnvironmentAuth


class DownloadTests(TestCase):
    def test_denies_unknown_client(self):
        with self.given_clients({'client1': 'id1'}):
            message, status = client_read.download('unknown_client')
            self.assertEqual(status, 403)

    @contextmanager
    def given_clients(self, clients):
        original_clients = client_read.CLIENTS
        client_read.CLIENTS = EnvironmentAuth(clients)
        yield
        client_read.CLIENTS = original_clients
