text: from unittest import mock
  
  from django.contrib.auth import get_user_model
  from graphql_relay.node.node import to_global_id
  
  from bilbyui.models import BilbyJob
  from bilbyui.tests.testcases import BilbyTestCase
  
  User = get_user_model()
  
  
  class TestQueriesWithAuthenticatedUser(BilbyTestCase):
      def setUp(self):
          self.maxDiff = 9999
  
          self.user = User.objects.create(username="buffy", first_name="buffy", last_name="summers")
          self.client.authenticate(self.user)
  
      def perform_db_search_mock(*args, **kwargs):
          return True, [
              {
                  'user': {
                      'id': 1,
                      'firstName': 'buffy',
                      'lastName': 'summers'
                  },
                  'job': {
                      'id': 1,
                      'name': 'Test1',
                      'description': 'A test job'
                  },
                  'history': [{'state': 500, 'timestamp': '2020-01-01 12:00:00 UTC'}],
              },
              {
                  'user': {
                      'id': 1,
                      'firstName': 'buffy',
                      'lastName': 'summers'
                  },
                  'job': {
                      'id': 2,
                      'name': 'Test2',
                      'description': ''
                  },
                  'history': [{'state': 500, 'timestamp': '2020-01-01 12:00:00 UTC'}],
              }
          ]
  
      def test_bilby_job_query(self):
          """
          bilbyJob node query should return a single job for an authenticated user."
          """
          job = BilbyJob.objects.create(user_id=self.user.id)
          job.ini_string = """detectors=['H1']"""
          job.save()
          global_id = to_global_id("BilbyJobNode", job.id)
          response = self.client.execute(
              f"""
              query {{
                  bilbyJob(id:"{global_id}"){{
                      id
                      name
                      userId
                      description
                      jobControllerId
                      private
                      lastUpdated
                      params {{
                          details {{
                              name
                              description
                              private
                          }}
                      }}
                  }}
              }}
              """
          )
          expected = {
              "bilbyJob": {
                  "id": "QmlsYnlKb2JOb2RlOjE=",
                  "name": "",
                  "userId": 1,
                  "description": None,
                  "jobControllerId": None,
                  "private": False,
                  "lastUpdated": job.last_updated.strftime("%Y-%m-%d %H:%M:%S UTC"),
                  "params": {
                      "details": {"name": "", "description": None, "private": False}
                  }
              }
          }
          self.assertDictEqual(
              expected, response.data, "bilbyJob query returned unexpected data."
          )
  
      def test_bilby_jobs_query(self):
          """
          bilbyJobs query should return a list of personal jobs for an authenticated user.
          """
          BilbyJob.objects.create(
              user_id=self.user.id,
              name="Test1",
              job_controller_id=2,
              is_ligo_job=False
          )
          BilbyJob.objects.create(
              user_id=self.user.id,
              name="Test2",
              job_controller_id=1,
              description="A test job",
              is_ligo_job=False
          )
          # This job shouldn't appear in the list because it belongs to another user.
          BilbyJob.objects.create(user_id=4, name="Test3", job_controller_id=3)
          response = self.client.execute(
              """
              query {
                  bilbyJobs {
                      edges {
                          node {
                              userId
                              name
                              description
                          }
                      }
                  }
              }
              """
          )
          expected = {
              "bilbyJobs": {
                  "edges": [
                      {
                          "node": {
                              "userId": 1,
                              "name": "Test1",
                              "description": None
                          }
                      },
                      {
                          "node": {
                              "userId": 1,
                              "name": "Test2",
                              "description": "A test job",
                          }
                      },
                  ]
              }
          }
          self.assertDictEqual(
              response.data, expected, "bilbyJobs query returned unexpected data."
          )
  
      @mock.patch('bilbyui.schema.perform_db_search', side_effect=perform_db_search_mock)
      def test_public_bilby_jobs_query(self, perform_db_search):
          BilbyJob.objects.create(
              user_id=self.user.id, name="Test1", description="first job", job_controller_id=2, private=False
          )
          BilbyJob.objects.create(
              user_id=self.user.id, name="Test2", job_controller_id=1, description="A test job", private=False
          )
          # This job shouldn't appear in the list because it's private.
          BilbyJob.objects.create(user_id=4, name="Test3", job_controller_id=3, private=True)
          response = self.client.execute(
              """
              query {
                  publicBilbyJobs(search:"", timeRange:"all") {
                      edges {
                          node {
                              user
                              description
                              name
                              jobStatus {
                               name
                              }
                              timestamp
                              id
                          }
                       }
                   }
               }
               """
          )
  
          expected = {
              'publicBilbyJobs': {
                  'edges': [
                      {
                          'node': {
                              'description': 'A test job',
                              'id': 'QmlsYnlKb2JOb2RlOjE=',
                              'name': 'Test1',
                              'jobStatus': {
                                  'name': 'Completed'
                              },
                              'timestamp': '2020-01-01 12:00:00 UTC',
                              'user': 'buffy summers'
                          }
                      },
                      {
                          'node': {
                              'description': '',
                              'id': 'QmlsYnlKb2JOb2RlOjI=',
                              'name': 'Test2',
                              'jobStatus': {
                                  'name': 'Completed',
                              },
                              'timestamp': '2020-01-01 12:00:00 UTC',
                              'user': 'buffy summers'
                          }
                      }
                  ]
              }
          }
          self.assertDictEqual(response.data, expected, "publicBilbyJobs query returned unexpected data.")
  