1 package mocks
2
3 import (
4 "encoding/json"
5 "strings"
6
7 "edge-infra.dev/pkg/edge/api/graph/model"
8 )
9
10 type CreateOperatorInterventionCommandResponse struct {
11 CreateOperatorInterventionCommand model.CreateOperatorInterventionCommandResponse `json:"createOperatorInterventionCommands"`
12 }
13
14 func CreateOperatorInterventionCommand(body []byte) (interface{}, bool, error) {
15 if !strings.Contains(string(body), "createOperatorInterventionCommands(commands: $commands){errors{type,command,privilege,role}}") {
16 return nil, false, nil
17 }
18 return CreateOperatorInterventionCommandResponse{}, true, nil
19 }
20
21 type CreateOperatorInterventionPrivilegeResponse struct {
22 CreateOperatorInterventionPrivilege model.CreateOperatorInterventionPrivilegeResponse `json:"createOperatorInterventionPrivileges"`
23 }
24
25 func CreateOperatorInterventionPrivilege(body []byte) (interface{}, bool, error) {
26 if !strings.Contains(string(body), "createOperatorInterventionPrivileges(privileges: $privileges){errors{type,command,privilege,role}}") {
27 return nil, false, nil
28 }
29 return CreateOperatorInterventionPrivilegeResponse{}, true, nil
30 }
31
32 type UpdateOperatorInterventionRuleResponse struct {
33 UpdateOperatorInterventionRule model.UpdateOperatorInterventionRuleResponse `json:"updateOperatorInterventionRules"`
34 }
35
36 func UpdateOperatorInterventionRule(body []byte) (interface{}, bool, error) {
37 if !strings.Contains(string(body), "updateOperatorInterventionRules(rules: $rules){errors{type,command,privilege,role}}") {
38 return UpdateOperatorInterventionRuleResponse{}, false, nil
39 }
40
41 var m struct {
42 Query string
43 Variables struct {
44 Rules []struct {
45 Privilege struct {
46 Name string
47 }
48 Commands []*struct {
49 Name string
50 }
51 }
52 }
53 }
54 err := json.Unmarshal(body, &m)
55 if err != nil {
56 return UpdateOperatorInterventionRuleResponse{}, true, err
57 }
58
59 var resp UpdateOperatorInterventionRuleResponse
60
61 privilege := m.Variables.Rules[0].Privilege.Name
62 if privilege == "unknown-privilege" {
63 resp.UpdateOperatorInterventionRule.Errors = append(resp.UpdateOperatorInterventionRule.Errors, &model.OperatorInterventionErrorResponse{
64 Type: model.OperatorInterventionErrorTypeUnknownPrivilege,
65 Privilege: &privilege,
66 })
67 }
68 return resp, true, nil
69 }
70
71 type UpdateOperatorInterventionRoleMappingResponse struct {
72 UpdateOperatorInterventionRoleMapping model.UpdateOperatorInterventionRoleMappingResponse `json:"updateOperatorInterventionRoleMappings"`
73 }
74
75 func UpdateOperatorInterventionRoleMapping(body []byte) (interface{}, bool, error) {
76 if !strings.Contains(string(body), "updateOperatorInterventionRoleMappings(roleMappings: $roleMappings){errors{type,command,privilege,role}}") {
77 return nil, false, nil
78 }
79
80 var m struct {
81 Query string
82 Variables struct {
83 RoleMappings []struct {
84 Role string
85 Privileges []struct {
86 Name string
87 }
88 }
89 }
90 }
91 err := json.Unmarshal(body, &m)
92 if err != nil {
93 return UpdateOperatorInterventionRoleMappingResponse{}, true, err
94 }
95
96 var resp UpdateOperatorInterventionRoleMappingResponse
97
98 if role := m.Variables.RoleMappings[0].Role; role == "unknown-role" {
99 resp.UpdateOperatorInterventionRoleMapping.Errors = append(resp.UpdateOperatorInterventionRoleMapping.Errors, &model.OperatorInterventionErrorResponse{
100 Type: model.OperatorInterventionErrorTypeUnknownRole,
101 Role: &role,
102 })
103 }
104 return resp, true, nil
105 }
106
View as plain text