Changes api and views to match new checks rework. Fixed the Vue tests to match the new data

This commit is contained in:
Josh Krawczyk 2020-05-30 11:38:51 -04:00
parent d298f375c2
commit 466c315967
15 changed files with 145 additions and 194 deletions

View File

@ -3,6 +3,7 @@ from rest_framework import serializers
from .models import Policy
from autotasks.models import AutomatedTask
from autotasks.serializers import TaskSerializer
from checks.serializers import CheckSerializer
class PolicySerializer(serializers.ModelSerializer):
@ -29,3 +30,4 @@ class AutoTaskPolicySerializer(serializers.ModelSerializer):
"name",
"autotasks",
)

View File

@ -6,6 +6,7 @@ urlpatterns = [
path("policies/<int:pk>/related/", views.GetRelated.as_view()),
path("policies/overview/", views.OverviewPolicy.as_view()),
path("policies/<pk>/", views.GetUpdateDeletePolicy.as_view()),
path("<int:pk>/policychecks/", views.PolicyCheck.as_view()),
path("<int:pk>/policyautomatedtasks/", views.PolicyAutoTask.as_view()),
path("runwintask/<int:pk>/", views.RunPolicyTask.as_view()),
]

View File

@ -13,6 +13,7 @@ from .models import Policy
from agents.models import Agent
from scripts.models import Script
from clients.models import Client, Site
from checks.models import Check
from clients.serializers import (
ClientSerializer,
@ -27,6 +28,8 @@ from .serializers import (
AutoTaskPolicySerializer,
)
from checks.serializers import CheckSerializer
class GetAddPolicies(APIView):
def get(self, request):
@ -112,6 +115,11 @@ class RunPolicyTask(APIView):
# TODO: Run tasks for all Agents under policy
return Response("ok")
class PolicyCheck(APIView):
def get(self, request, pk):
checks = Check.objects.filter(policy__pk=pk)
return Response(CheckSerializer(checks, many=True).data)
class OverviewPolicy(APIView):
def get(self, request):

View File

@ -57,29 +57,3 @@ class CheckSerializer(serializers.ModelSerializer):
)
return val
""" class PolicyChecksSerializer(serializers.ModelSerializer):
diskchecks = DiskCheckSerializer(many=True, read_only=True)
cpuloadchecks = CpuLoadCheckSerializer(many=True, read_only=True)
memchecks = MemCheckSerializer(many=True, read_only=True)
pingchecks = PingCheckSerializer(many=True, read_only=True)
winservicechecks = WinServiceCheckSerializer(many=True, read_only=True)
scriptchecks = ScriptCheckSerializer(many=True, read_only=True)
eventlogchecks = EventLogCheckSerializer(many=True, read_only=True)
class Meta:
model = Policy
fields = (
"id",
"name",
"active",
"diskchecks",
"cpuloadchecks",
"memchecks",
"pingchecks",
"winservicechecks",
"scriptchecks",
"eventlogchecks",
)
"""

View File

@ -5,7 +5,6 @@ urlpatterns = [
path("checks/", views.GetAddCheck.as_view()),
path("<int:pk>/check/", views.GetUpdateDeleteCheck.as_view()),
path("<pk>/loadchecks/", views.load_checks),
path("<pk>/loadpolicychecks/", views.load_policy_checks),
path("checkrunner/", views.check_runner),
path("getalldisks/", views.get_disks_for_policies),
path("runchecks/<pk>/", views.run_checks),

View File

@ -157,12 +157,6 @@ def load_checks(request, pk):
return Response(CheckSerializer(checks, many=True).data)
@api_view()
def load_policy_checks(request, pk):
policy = get_object_or_404(Policy, pk=pk)
return Response(PolicyChecksSerializer(policy).data)
@api_view()
def get_disks_for_policies(request):
return Response(DiskCheck.all_disks())
return Response(Check.all_disks())

View File

@ -247,7 +247,7 @@ export default {
},
clearRow() {
this.$store.commit("automation/setSelectedPolicy", null);
this.$store.commit("automation/setPolicyChecks", {});
this.$store.commit("automation/setPolicyChecks", []);
this.$store.commit("automation/setPolicyAutomatedTasks", {});
},
refresh() {

View File

@ -1,5 +1,5 @@
<template>
<div v-if="Object.keys(checks).length === 0">No Policy Selected</div>
<div v-if="selectedPolicy === null">No Policy Selected</div>
<div class="row" v-else>
<div class="col-12">
<q-btn
@ -12,43 +12,43 @@
>
<q-menu>
<q-list dense style="min-width: 200px">
<q-item clickable v-close-popup @click="showAddDialog('AddDiskSpaceCheck')">
<q-item clickable v-close-popup @click="showAddDialog('DiskSpaceCheck')">
<q-item-section side>
<q-icon size="xs" name="far fa-hdd" />
</q-item-section>
<q-item-section>Disk Space Check</q-item-section>
</q-item>
<q-item clickable v-close-popup @click="showAddDialog('AddPingCheck')">
<q-item clickable v-close-popup @click="showAddDialog('PingCheck')">
<q-item-section side>
<q-icon size="xs" name="fas fa-network-wired" />
</q-item-section>
<q-item-section>Ping Check</q-item-section>
</q-item>
<q-item clickable v-close-popup @click="showAddDialog('AddCpuLoadCheck')">
<q-item clickable v-close-popup @click="showAddDialog('CpuLoadCheck')">
<q-item-section side>
<q-icon size="xs" name="fas fa-microchip" />
</q-item-section>
<q-item-section>CPU Load Check</q-item-section>
</q-item>
<q-item clickable v-close-popup @click="showAddDialog('AddMemCheck')">
<q-item clickable v-close-popup @click="showAddDialog('MemCheck')">
<q-item-section side>
<q-icon size="xs" name="fas fa-memory" />
</q-item-section>
<q-item-section>Memory Check</q-item-section>
</q-item>
<q-item clickable v-close-popup @click="showAddDialog('AddWinSvcCheck')">
<q-item clickable v-close-popup @click="showAddDialog('WinSvcCheck')">
<q-item-section side>
<q-icon size="xs" name="fas fa-cogs" />
</q-item-section>
<q-item-section>Windows Service Check</q-item-section>
</q-item>
<q-item clickable v-close-popup @click="showAddDialog('AddScriptCheck')">
<q-item clickable v-close-popup @click="showAddDialog('ScriptCheck')">
<q-item-section side>
<q-icon size="xs" name="fas fa-terminal" />
</q-item-section>
<q-item-section>Script Check</q-item-section>
</q-item>
<q-item clickable v-close-popup @click="showAddDialog('AddEventLogCheck')">
<q-item clickable v-close-popup @click="showAddDialog('EventLogCheck')">
<q-item-section side>
<q-icon size="xs" name="fas fa-clipboard-list" />
</q-item-section>
@ -61,18 +61,18 @@
dense
flat
push
@click="onRefresh(checks.id)"
@click="onRefresh(selectedPolicy)"
icon="refresh"
ref="refresh"
/>
<template v-if="allChecks === undefined || allChecks.length === 0">
<template v-if="checks.length === 0">
<p>No Checks</p>
</template>
<template v-else>
<q-table
dense
class="tabs-tbl-sticky"
:data="allChecks"
:data="checks"
:columns="columns"
:row-key="row => row.id + row.check_type"
binary-state-sort
@ -99,14 +99,14 @@
</template>
<!-- body slots -->
<template v-slot:body="props">
<q-tr @contextmenu="editCheckPK = props.row.id" :props="props">
<q-tr :props="props">
<!-- context menu -->
<q-menu context-menu>
<q-list dense style="min-width: 200px">
<q-item
clickable
v-close-popup
@click="showEditDialog(props.row.check_type)"
@click="showEditDialog(props.row)"
id="context-edit"
>
<q-item-section side>
@ -117,7 +117,7 @@
<q-item
clickable
v-close-popup
@click="deleteCheck(props.row.id, props.row.check_type)"
@click="deleteCheck(props.row)"
id="context-delete"
>
<q-item-section side>
@ -195,8 +195,9 @@
v-if="dialogComponent !== null"
:is="dialogComponent"
@close="hideDialog"
:policypk="checks.id"
:editCheckPK="editCheckPK"
:policypk="selectedPolicy"
:checkpk="editCheckPK"
:mode="!!editCheckPK ? 'edit' : 'add'"
/>
</q-dialog>
</div>
@ -206,39 +207,25 @@
import { mapState, mapGetters } from "vuex";
import mixins, { notifySuccessConfig, notifyErrorConfig } from "@/mixins/mixins";
import PolicyStatus from "@/components/automation/modals/PolicyStatus";
import AddDiskSpaceCheck from "@/components/modals/checks/AddDiskSpaceCheck";
import EditDiskSpaceCheck from "@/components/modals/checks/EditDiskSpaceCheck";
import AddPingCheck from "@/components/modals/checks/AddPingCheck";
import EditPingCheck from "@/components/modals/checks/EditPingCheck";
import AddCpuLoadCheck from "@/components/modals/checks/AddCpuLoadCheck";
import EditCpuLoadCheck from "@/components/modals/checks/EditCpuLoadCheck";
import AddMemCheck from "@/components/modals/checks/AddMemCheck";
import EditMemCheck from "@/components/modals/checks/EditMemCheck";
import AddWinSvcCheck from "@/components/modals/checks/AddWinSvcCheck";
import EditWinSvcCheck from "@/components/modals/checks/EditWinSvcCheck";
import AddScriptCheck from "@/components/modals/checks/AddScriptCheck";
import EditScriptCheck from "@/components/modals/checks/EditScriptCheck";
import AddEventLogCheck from "@/components/modals/checks/AddEventLogCheck";
import EditEventLogCheck from "@/components/modals/checks/EditEventLogCheck";
import DiskSpaceCheck from "@/components/modals/checks/DiskSpaceCheck";
import PingCheck from "@/components/modals/checks/PingCheck";
import CpuLoadCheck from "@/components/modals/checks/CpuLoadCheck";
import MemCheck from "@/components/modals/checks/MemCheck";
import WinSvcCheck from "@/components/modals/checks/WinSvcCheck";
import ScriptCheck from "@/components/modals/checks/ScriptCheck";
import EventLogCheck from "@/components/modals/checks/EventLogCheck";
export default {
name: "PolicyChecksTab",
components: {
PolicyStatus,
AddDiskSpaceCheck,
EditDiskSpaceCheck,
AddPingCheck,
EditPingCheck,
AddCpuLoadCheck,
EditCpuLoadCheck,
AddMemCheck,
EditMemCheck,
AddWinSvcCheck,
EditWinSvcCheck,
AddScriptCheck,
EditScriptCheck,
AddEventLogCheck,
EditEventLogCheck
DiskSpaceCheck,
PingCheck,
CpuLoadCheck,
MemCheck,
WinSvcCheck,
ScriptCheck,
EventLogCheck,
},
mixins: [mixins],
data() {
@ -286,33 +273,33 @@ export default {
this.dialogComponent = component;
this.showDialog = true;
},
showEditDialog(category) {
switch (category) {
showEditDialog(check) {
switch (check.check_type) {
case "diskspace":
this.dialogComponent = "EditDiskSpaceCheck";
this.dialogComponent = "DiskSpaceCheck";
break;
case "ping":
this.dialogComponent = "EditPingCheck";
this.dialogComponent = "PingCheck";
break;
case "cpuload":
this.dialogComponent = "EditCpuLoadCheck";
this.dialogComponent = "CpuLoadCheck";
break;
case "memory":
this.dialogComponent = "EditMemCheck";
this.dialogComponent = "MemCheck";
break;
case "winsvc":
this.dialogComponent = "EditWinSvcCheck";
this.dialogComponent = "WinSvcCheck";
break;
case "script":
this.dialogComponent = "EditScriptCheck";
this.dialogComponent = "ScriptCheck";
break;
case "eventlog":
this.dialogComponent = "EditEventLogCheck";
this.dialogComponent = "EventLogCheck";
break;
default:
return null;
}
this.editCheckPK = check.id
this.showDialog = true;
},
@ -320,22 +307,22 @@ export default {
this.showDialog = false;
this.dialogComponent = null;
},
deleteCheck(pk, check_type) {
deleteCheck(check) {
this.$q
.dialog({
title: `Delete ${check_type} check`,
title: `Delete ${check.check_type} check`,
ok: { label: "Delete", color: "negative" },
cancel: true,
})
.onOk(() => {
const data = { pk: pk, checktype: check_type };
const data = { pk: check.id, checktype: check.check_type };
this.$store
.dispatch("deleteCheck", data)
.then(r => {
this.$store.dispatch("automation/loadPolicyChecks", this.checks.id);
this.$q.notify(notifySuccessConfig);
this.$store.dispatch("automation/loadPolicyChecks", check.id);
this.$q.notify(notifySuccessConfig("Check Deleted!"));
})
.catch(e => this.$q.notify(notifyErrorConfig));
.catch(e => this.$q.notify(notifyErrorConfig("An Error Occurred while deleting")));
});
},
showPolicyCheckStatusModal(check) {
@ -364,12 +351,10 @@ export default {
}
},
computed: {
...mapState({
checks: state => state.automation.checks
}),
...mapGetters({
allChecks: "automation/allChecks"
})
checks: "automation/checks",
selectedPolicy: "automation/selectedPolicyPk"
})
}
};
</script>

View File

@ -65,10 +65,6 @@
<script>
import axios from "axios";
<<<<<<< HEAD:web/src/components/modals/checks/ScriptCheck.vue
=======
import { mapState, mapGetters } from "vuex";
>>>>>>> More Vue Tests:web/src/components/modals/checks/AddScriptCheck.vue
import mixins from "@/mixins/mixins";
import { mapGetters, mapState } from "vuex";
export default {

View File

@ -4,22 +4,14 @@ export default {
namespaced: true,
state: {
selectedPolicy: null,
checks: {},
checks: [],
automatedTasks: {},
policies: [],
},
getters: {
allChecks(state) {
return [
...state.checks.diskchecks,
...state.checks.cpuloadchecks,
...state.checks.memchecks,
...state.checks.scriptchecks,
...state.checks.winservicechecks,
...state.checks.pingchecks,
...state.checks.eventlogchecks
];
checks(state) {
return state.checks;
},
selectedPolicyPk(state) {
return state.selectedPolicy;
@ -56,7 +48,7 @@ export default {
});
},
loadPolicyChecks(context, pk) {
axios.get(`/checks/${pk}/loadpolicychecks/`).then(r => {
axios.get(`/automation/${pk}/policychecks/`).then(r => {
context.commit("setPolicyChecks", r.data);
});
},

View File

@ -202,7 +202,7 @@ export const store = new Vuex.Store({
child_single.push({
label: sites_arr[i].split("|")[0],
id: sites_arr[i].split("|")[1],
raw: sites_arr[i],
raw: `Site|${sites_arr[i]}`,
header: "generic",
icon: "apartment",
iconColor: sites_arr[i].split("|")[2]
@ -211,7 +211,7 @@ export const store = new Vuex.Store({
output.push({
label: prop.split("|")[0],
id: prop.split("|")[1],
raw: prop,
raw: `Client|${prop}`,
header: "root",
icon: "business",
iconColor: prop.split("|")[2],

View File

@ -97,7 +97,7 @@ describe("AutomationManager.vue", () => {
expect(actions.loadPolicies).toHaveBeenCalled();
expect(mutations.setSelectedPolicy).toHaveBeenCalledWith(expect.anything(), null);
expect(mutations.setPolicyChecks).toHaveBeenCalledWith(expect.anything(), {});
expect(mutations.setPolicyChecks).toHaveBeenCalledWith(expect.anything(), []);
expect(mutations.setPolicyAutomatedTasks).toHaveBeenCalledWith(expect.anything(), {});
});
@ -206,7 +206,7 @@ describe("AutomationManager.vue", () => {
expect(bodyWrapper.find(".q-dialog").exists()).toBe(true);
expect(mutations.setSelectedPolicy).toHaveBeenCalledWith(expect.anything(), null);
expect(mutations.setPolicyChecks).toHaveBeenCalledWith(expect.anything(), {});
expect(mutations.setPolicyChecks).toHaveBeenCalledWith(expect.anything(), []);
expect(mutations.setPolicyAutomatedTasks).toHaveBeenCalledWith(expect.anything(), {});
});
@ -218,7 +218,7 @@ describe("AutomationManager.vue", () => {
button.trigger("click");
expect(actions.loadPolicies).toHaveBeenCalled();
expect(mutations.setSelectedPolicy).toHaveBeenCalledWith(expect.anything(), null);
expect(mutations.setPolicyChecks).toHaveBeenCalledWith(expect.anything(), {});
expect(mutations.setPolicyChecks).toHaveBeenCalledWith(expect.anything(), []);
expect(mutations.setPolicyAutomatedTasks).toHaveBeenCalledWith(expect.anything(), {});
});

View File

@ -30,13 +30,24 @@ afterEach(() => {
/*** TEST SUITES ***/
describe("PolicyChecksTab.vue with no policy selected", () => {
let wrapper, state, store;
let wrapper, state, getters, store;
// Runs before every test
beforeEach(() => {
// Create the Test store
// Create the Test store
state = {
checks: {},
checks: [],
selectedPolicy: null
};
getters = {
checks(state) {
return state.checks
},
selectedPolicyPk(state) {
return state.selectedPolicy
}
};
store = new Vuex.Store({
@ -44,6 +55,7 @@ describe("PolicyChecksTab.vue with no policy selected", () => {
automation: {
namespaced: true,
state,
getters
}
}
});
@ -67,13 +79,13 @@ describe("PolicyChecksTab.vue with policy selected and no checks", () => {
// Used for the add check test loop
const addChecksMenu = [
{ name: "AddDiskSpaceCheck", index: 0 },
{ name: "AddPingCheck", index: 1},
{ name: "AddCpuLoadCheck", index: 2},
{ name: "AddMemCheck", index: 3},
{ name: "AddWinSvcCheck", index: 4},
{ name: "AddScriptCheck", index: 5},
{ name: "AddEventLogCheck", index: 6}
{ name: "DiskSpaceCheck", index: 0 },
{ name: "PingCheck", index: 1},
{ name: "CpuLoadCheck", index: 2},
{ name: "MemCheck", index: 3},
{ name: "WinSvcCheck", index: 4},
{ name: "ScriptCheck", index: 5},
{ name: "EventLogCheck", index: 6}
];
let wrapper, store, state, actions, getters;
@ -82,30 +94,16 @@ describe("PolicyChecksTab.vue with policy selected and no checks", () => {
// Create the Test store
state = {
checks: {
id: 1,
name: "Test Policy",
diskchecks: [],
cpuloadchecks: [],
memchecks: [],
scriptchecks: [],
winservicechecks: [],
pingchecks: [],
eventlogchecks: []
}
checks: [],
selectedPolicy: 1
};
getters = {
allChecks(state) {
return [
...state.checks.diskchecks,
...state.checks.cpuloadchecks,
...state.checks.memchecks,
...state.checks.scriptchecks,
...state.checks.winservicechecks,
...state.checks.pingchecks,
...state.checks.eventlogchecks
];
checks(state) {
return state.checks
},
selectedPolicyPk(state) {
return state.selectedPolicy
}
};
@ -129,13 +127,13 @@ describe("PolicyChecksTab.vue with policy selected and no checks", () => {
store,
localVue,
stubs: [
"AddDiskSpaceCheck",
"AddPingCheck",
"AddCpuLoadCheck",
"AddMemCheck",
"AddWinSvcCheck",
"AddScriptCheck",
"AddEventLogCheck"
"DiskSpaceCheck",
"PingCheck",
"CpuLoadCheck",
"MemCheck",
"WinSvcCheck",
"ScriptCheck",
"EventLogCheck"
]
});
@ -179,45 +177,40 @@ describe("PolicyChecksTab.vue with policy selected and checks", () => {
// Used for the edit check test loop
const editChecksModals = [
{name: "EditDiskSpaceCheck", index: 0, id: 1},
{name: "EditCpuLoadCheck", index: 1, id: 2},
{name: "EditMemCheck", index: 2, id: 3},
{name: "EditScriptCheck", index: 3, id: 4},
{name: "EditWinSvcCheck", index: 4, id: 5},
{name: "EditPingCheck", index: 5, id: 6},
{name: "EditEventLogCheck", index: 6, id: 7}
{name: "DiskSpaceCheck", index: 0, id: 1},
{name: "CpuLoadCheck", index: 1, id: 2},
{name: "MemCheck", index: 2, id: 3},
{name: "ScriptCheck", index: 3, id: 4},
{name: "WinSvcCheck", index: 4, id: 5},
{name: "PingCheck", index: 5, id: 6},
{name: "EventLogCheck", index: 6, id: 7}
];
let state, rootActions, actions, getters, store, wrapper;
// Runs before every test
beforeEach(() => {
// Create the Test store
// Create the Test store
state = {
checks: {
id: 1,
name: "Test Policy",
diskchecks: [diskcheck],
cpuloadchecks: [cpuloadcheck],
memchecks: [memcheck],
scriptchecks: [scriptcheck],
winservicechecks: [winservicecheck],
pingchecks: [pingcheck],
eventlogchecks: [eventlogcheck]
},
checks: [
diskcheck,
cpuloadcheck,
memcheck,
scriptcheck,
winservicecheck,
pingcheck,
eventlogcheck
],
selectedPolicy: 1
};
getters = {
allChecks(state) {
return [
...state.checks.diskchecks,
...state.checks.cpuloadchecks,
...state.checks.memchecks,
...state.checks.scriptchecks,
...state.checks.winservicechecks,
...state.checks.pingchecks,
...state.checks.eventlogchecks
];
checks(state) {
return state.checks
},
selectedPolicyPk(state) {
return state.selectedPolicy
}
};
@ -247,13 +240,13 @@ describe("PolicyChecksTab.vue with policy selected and checks", () => {
store,
localVue,
stubs: [
"EditDiskSpaceCheck",
"EditPingCheck",
"EditCpuLoadCheck",
"EditMemCheck",
"EditWinSvcCheck",
"EditScriptCheck",
"EditEventLogCheck",
"DiskSpaceCheck",
"PingCheck",
"CpuLoadCheck",
"MemCheck",
"WinSvcCheck",
"ScriptCheck",
"EventLogCheck",
"PolicyStatus"
]
});

View File

@ -0,0 +1,8 @@
import { mount, shallowMount, createLocalVue, createWrapper } from "@vue/test-utils";
import PolicyAutomatedTasksTab from "@/components/automation/PolicyAutomatedTasksTab";
import Vuex from "vuex";
import "@/quasar.js";
describe.skip("PolicyAutomatedTasks.vue", () => {
// TODO after checks rework
});

View File

@ -1 +0,0 @@
// TODO after checks rework