Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
axiosmocks.js 13.80 KiB
import axios from "axios";
import MockAdapter from 'axios-mock-adapter';
import Reservation from '../__mocks__/reservation.data.json'
import SchedulingUnitBlueprintSlimWithTask from '../__mocks__/scheduling_unit_blueprint_slim_with_task.json'
import BluePrintExtended from  '../__mocks__/blueprintsextended.json'


export const fakeauthanswer = { data:{
    "count": 1,
    "next": "null",
    "previous": null,
    "results": [
      {
        "name": "nothing"
      }
    ]
  }}

export function MockAxiosBadSecurity(mock) {
  if (mock==null)  mock = new MockAdapter(axios);
  return MockAxiosSecurity(mock,500)
}


export function MockAxiosSecurity(mock, statusCode=200,iswWithSchedularWrite) {
    
    if (mock==null)  mock = new MockAdapter(axios);
    let AllowedSchedular="GET"
    if (iswWithSchedularWrite) AllowedSchedular="GET, PATCH"

    
    mock.onGet("/api/scheduling_set/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE, copy"});
     mock.onGet("/api/scheduling_unit_draft/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE, copy, create_cleanuptask_for_scheduling_unit_draft, create_observing_strategy_template, create_scheduling_unit_blueprint_and_tasks_and_subtasks_tree, scheduling_constraints_plot, specifications_doc, update_task_graph_from_specifications_doc"});
     mock.onGet("/api/project/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE, friend, my_roles, report"});
     mock.onGet("/api/scheduling_set/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE, copy"});
     mock.onGet("/api/scheduling_unit_observing_strategy_template/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE, create_scheduling_unit, template_doc_complete_with_defaults, template_doc_with_just_the_parameters, trigger_doc"});
     mock.onGet("/api/scheduling_unit_blueprint/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE, cancel, copy_specifications_doc_back_into_draft, copy_specifications_doc_including_copies_for_failed_tasks_back_into_draft, copy_to_draft, create_cleanuptask_for_scheduling_unit_blueprint, create_copies_of_failed_tasks_via_draft, get_all_subtasks_log_urls, mark_as_obsolete, mark_dynamically_scheduled, mark_fixed_time_scheduled_at_scheduled_starttime, reset_schedule, schedule, scheduling_constraints_plot, specifications_doc, specifications_doc_including_copies_for_failed_tasks, specifications_doc_with_just_the_parameters, subtasks, system_events, unschedule"});
     mock.onGet("/api/cycle/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE, report "});
     mock.onGet("/api/task_draft/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE, copy, create_task_blueprint, create_task_blueprint_and_subtasks, predecessors, successors"});
     mock.onGet("/api/task_blueprint/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, cancel, copy_to_new_draft, create_subtasks, mark_as_obsolete, predecessors, restart_when_on_hold, schedule_independent_subtasks, successors, system_events"});
     mock.onGet("/api/reservation/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE"});
     mock.onGet("/api/task_relation_draft/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE"});
     mock.onGet("/api/task_relation_blueprint/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE"});
     mock.onGet("/api/system_event/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE"});
     mock.onGet("/api/setting/fixed_time_scheduling_enabled/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":AllowedSchedular});
     mock.onGet("/api/setting/dynamic_scheduling_enabled/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":AllowedSchedular});
     mock.onGet("/api/project/?fields=name&limit=1").reply(statusCode, fakeauthanswer,{"access-control-allow-methods":"GET, POST, PUT, PATCH, DELETE, friend, my_roles, report"});
    return mock;
}

const combinedFakeSun =    {
        "CS002": {
            "sunrise": [
                {
                    "start": "2023-12-03T06:14:16",
                    "end": "2023-12-03T09:14:01"
                },
                {
                    "start": "2023-12-04T06:15:29",
                    "end": "2023-12-04T09:16:08"
                },
                {
                    "start": "2023-12-05T06:16:40",
                    "end": "2023-12-05T09:18:12"
                },
                {
                    "start": "2023-12-06T06:17:49",
                    "end": "2023-12-06T09:20:15"
                },
                {
                    "start": "2023-12-07T06:18:56",
                    "end": "2023-12-07T09:22:16"
                },
                {
                    "start": "2023-12-08T06:20:01",
                    "end": "2023-12-08T09:24:12"
                },
                {
                    "start": "2023-12-09T06:21:03",
                    "end": "2023-12-09T09:26:04"
                }
            ],
            "sunset": [
                {
                    "start": "2023-12-03T13:30:04",
                    "end": "2023-12-03T16:29:45"
                },
                {
                    "start": "2023-12-04T13:28:40",
                    "end": "2023-12-04T16:29:21"
                },
                {
                    "start": "2023-12-05T13:27:21",
                    "end": "2023-12-05T16:29:01"
                },
                {
                    "start": "2023-12-06T13:26:08",
                    "end": "2023-12-06T16:28:43"
                },
                {
                    "start": "2023-12-07T13:25:01",
                    "end": "2023-12-07T16:28:28"
                },
                {
                    "start": "2023-12-08T13:23:59",
                    "end": "2023-12-08T16:28:16"
                },
                {
                    "start": "2023-12-09T13:23:03",
                    "end": "2023-12-09T16:28:07"
                }
            ],
            "day": [
                {
                    "start": "2023-12-03T09:14:01",
                    "end": "2023-12-03T13:30:04"
                },
                {
                    "start": "2023-12-04T09:16:08",
                    "end": "2023-12-04T13:28:40"
                },
                {
                    "start": "2023-12-05T09:18:12",
                    "end": "2023-12-05T13:27:21"
                },
                {
                    "start": "2023-12-06T09:20:15",
                    "end": "2023-12-06T13:26:08"
                },
                {
                    "start": "2023-12-07T09:22:16",
                    "end": "2023-12-07T13:25:01"
                },
                {
                    "start": "2023-12-08T09:24:12",
                    "end": "2023-12-08T13:23:59"
                },
                {
                    "start": "2023-12-09T09:26:04",
                    "end": "2023-12-09T13:23:03"
                }
            ],
            "night": [
                {
                    "start": "2023-12-02T16:30:11",
                    "end": "2023-12-03T06:14:16"
                },
                {
                    "start": "2023-12-03T16:29:45",
                    "end": "2023-12-04T06:15:29"
                },
                {
                    "start": "2023-12-04T16:29:21",
                    "end": "2023-12-05T06:16:40"
                },
                {
                    "start": "2023-12-05T16:29:01",
                    "end": "2023-12-06T06:17:49"
                },
                {
                    "start": "2023-12-06T16:28:43",
                    "end": "2023-12-07T06:18:56"
                },
                {
                    "start": "2023-12-07T16:28:28",
                    "end": "2023-12-08T06:20:01"
                },
                {
                    "start": "2023-12-08T16:28:16",
                    "end": "2023-12-09T06:21:03"
                }
            ]
        }
    }



const FakeSun = {
  data: {
      "CS002": {
          "sunrise": [
              {
                  "start": "2023-12-03T06:14:15.958",
                  "end": "2023-12-03T09:14:01.168"
              }
          ],
          "sunset": [
              {
                  "start": "2023-12-03T13:30:02.095",
                  "end": "2023-12-03T16:29:44.625"
              }
          ],
          "day": [
              {
                  "start": "2023-12-03T09:14:01.168",
                  "end": "2023-12-03T13:30:02.095"
              }
          ],
          "night": [
              {
                  "start": "2023-12-02T06:13:01.214",
                  "end": "2023-12-03T06:14:15.958"
              }
          ]
      }
  }
}

const FakeProjectState = {
  data: {
      "count": 5,
      "next": null,
      "previous": null,
      "results": [
          {
              "value": "opened",
              "url": "https://tmss.lofar.eu/api/project_state/opened"
          },
          {
              "value": "active",
              "url": "https://tmss.lofar.eu/api/project_state/active"
          },
          {
              "value": "finished",
              "url": "https://tmss.lofar.eu/api/project_state/finished"
          },
          {
              "value": "cancelled",
              "url": "https://tmss.lofar.eu/api/project_state/cancelled"
          },
          {
              "value": "suspended",
              "url": "https://tmss.lofar.eu/api/project_state/suspended"
          }
      ]
  }
}

const FakeSchedular = {
  data: {
      "name": "scheduler",
      "url": "https://tmss.lofar.eu/api/subsystem/scheduler",
      "created_at": "2022-10-03T08:55:11.449584",
      "description": "The scheduler subsystem takes care of scheduling all scheduling_units taking into account all their constraints and station availability",
      "status": "https://tmss.lofar.eu/api/subsystem_status/active",
      "status_value": "active",
      "tags": [],

      "updated_at": "2023-12-03T04:00:33.423663"
  }
}


const FakeProject = {
  data: {
      "count": 6,
      "next": null,
      "previous": null,
      "results": [
          {
              "name": "Calibration",
              "project_state_value": "active"
          },
          {
              "name": "COM18_001",
              "project_state_value": "opened"
          },
          {
              "name": "COM_COBALT2",
              "project_state_value": "active"
          },
          {
              "name": "DDT18_001",
              "project_state_value": "finished"
          },

          {
              "name": "high",
              "project_state_value": "suspended"
          },

          {
              "name": "LC18_019",
              "project_state_value": "opened"
          }

      ]
  }
}

const FakeFixedTimeScheduler = {
  data: {
      "url": "https://tmss.lofar.eu/api/setting/fixed_time_scheduling_enabled",
      "tags": [],
      "created_at": "2022-01-20T12:00:00",
      "updated_at": "2023-11-28T09:36:30.783393",
      "value": true
  }
}

const FakeDynamicSchedular = {
  data: {
      "url": "https://tmss.lofar.eu/api/setting/dynamic_scheduling_enabled",
      "tags": [],
      "created_at": "2021-10-11T10:54:30.647932",
      "updated_at": "2023-11-28T09:36:35.081048",
      "value": true
  }
}
export function MakeWeekViewPrimaryMock(mock) {
  if(!mock) mock = new MockAdapter(axios);
  mock.onGet("/api/util/sun_rise_and_set?stations=CS002&timestamps=2023-12-03").reply(200, FakeSun);
  mock.onGet("api/util/sun_rise_and_set?stations=CS002&timestamps=2023-08-06").reply(200, FakeSun);
  mock.onGet("/api/project_state/").reply(200, FakeProjectState);
  mock.onGet("/api/subsystem/scheduler").reply(200, FakeSchedular);
  mock.onGet("/api/project/?fields=name,project_state_value&limit=150").reply(200, FakeProject);
  mock.onGet("/api/setting/fixed_time_scheduling_enabled").reply(200, FakeFixedTimeScheduler);
  mock.onGet("/api/reservation_template").reply(200, { data: { Reservation } });
  mock.onGet("/api/setting/dynamic_scheduling_enabled").reply(200, FakeDynamicSchedular);
  mock.onGet(/\/api\/scheduling_unit_blueprint_slim_with_tasks\/\?process_start_time_before=/).reply(200, SchedulingUnitBlueprintSlimWithTask);

  
  
  mock.onGet("/api/scheduling_unit_blueprint_slim_with_tasks/?id=&limit=12&offset=38&process_start_time_before=2023-12-09+23%3A59%3A59&process_stop_time_after=2023-12-03+00%3A00%3A00").reply(200, SchedulingUnitBlueprintSlimWithTask);
  mock.onGet("/api/scheduling_unit_blueprint_slim_with_tasks/?process_start_time_before=2023-12-03 23:59:59&process_stop_time_after=2023-12-03 00:00:00&id=&limit=50&offset=12").reply(200, SchedulingUnitBlueprintSlimWithTask);
  mock.onOptions("/api/scheduling_unit_blueprint_extended/").reply(200, BluePrintExtended);
  mock.onGet("/api/util/lst?timestamp=2023-12-03T00:00:00&stations=CS002").reply(200, { data: { "UTC": "2023-12-03T00:00:00Z", "LST": { "CS002": "05:13:44" } } });
  mock.onGet("/api/util/sun_rise_and_set?stations=CS002&timestamps=2023-12-03,2023-12-04,2023-12-05,2023-12-06,2023-12-07,2023-12-08,2023-12-09").reply(200, combinedFakeSun);
  mock.onGet("/api/util/sun_rise_and_set?stations=CS002&timestamps=2023-08-06,2023-08-07,2023-08-08,2023-08-09,2023-08-10,2023-08-11,2023-08-12").reply(200, combinedFakeSun);
  
  
  
  return mock;
}