| 1 | import { Given, Then, When } from "@badeball/cypress-cucumber-preprocessor"; |
| 2 | import { |
| 3 | entryExists, |
| 4 | entryDoesNotExist, |
| 5 | searchForEntry, |
| 6 | selectEntry, |
| 7 | } from "../common/data_tables"; |
| 8 | import { navigateTo } from "../common/navigation"; |
| 9 | import { loginAsAdmin, logout } from "../common/authentication"; |
| 10 | |
| 11 | const fillDnsZone = (zone: string) => { |
| 12 | cy.dataCy("modal-textbox-dns-zone-name").type(zone); |
| 13 | cy.dataCy("modal-textbox-dns-zone-name").should("have.value", zone); |
| 14 | }; |
| 15 | |
| 16 | const fillReversedDnsZone = (ip: string) => { |
| 17 | cy.dataCy("modal-textbox-reverse-zone-ip").type(ip); |
| 18 | cy.dataCy("modal-textbox-reverse-zone-ip").should("have.value", ip); |
| 19 | }; |
| 20 | |
| 21 | const fillDnsZoneWithSkipOverlapCheck = (zone: string) => { |
| 22 | fillDnsZone(zone); |
| 23 | cy.dataCy("modal-checkbox-skip-overlap-check").click(); |
| 24 | cy.dataCy("modal-checkbox-skip-overlap-check").should("be.checked"); |
| 25 | }; |
| 26 | |
| 27 | const fillReversedDnsZoneWithSkipOverlapCheck = (ip: string) => { |
| 28 | fillReversedDnsZone(ip); |
| 29 | cy.dataCy("modal-checkbox-skip-overlap-check").click(); |
| 30 | cy.dataCy("modal-checkbox-skip-overlap-check").should("be.checked"); |
| 31 | }; |
| 32 | |
| 33 | export const createDnsZone = (zone: string) => { |
| 34 | cy.dataCy("dns-zones-button-add").click(); |
| 35 | cy.dataCy("add-dns-zone-modal").should("exist"); |
| 36 | |
| 37 | fillDnsZone(zone); |
| 38 | |
| 39 | cy.intercept({ method: "POST", url: "**/ipa/session/json" }, (req) => { |
| 40 | if (req.body.method === "dnszone_add") { |
| 41 | req.alias = "apiCall"; |
| 42 | } |
| 43 | }); |
| 44 | cy.dataCy("modal-button-add").click(); |
| 45 | cy.wait("@apiCall"); |
| 46 | cy.dataCy("add-dns-zone-modal").should("not.exist"); |
| 47 | }; |
| 48 | |
| 49 | const createReversedDnsZone = (ip: string) => { |
| 50 | cy.dataCy("dns-zones-button-add").click(); |
| 51 | cy.dataCy("add-dns-zone-modal").should("exist"); |
| 52 | cy.dataCy("modal-radio-reverse-zone-ip").click(); |
| 53 | cy.dataCy("modal-radio-reverse-zone-ip").should("be.checked"); |
| 54 | |
| 55 | fillReversedDnsZone(ip); |
| 56 | |
| 57 | cy.dataCy("modal-button-add").click(); |
| 58 | cy.dataCy("add-dns-zone-modal").should("not.exist"); |
| 59 | }; |
| 60 | |
| 61 | const createDnsZoneWithSkipOverlapCheck = (zone: string) => { |
| 62 | cy.dataCy("dns-zones-button-add").click(); |
| 63 | cy.dataCy("add-dns-zone-modal").should("exist"); |
| 64 | |
| 65 | fillDnsZoneWithSkipOverlapCheck(zone); |
| 66 | |
| 67 | cy.dataCy("modal-button-add").click(); |
| 68 | cy.dataCy("add-dns-zone-modal").should("not.exist"); |
| 69 | }; |
| 70 | |
| 71 | const createReversedDnsZoneWithSkipOverlapCheck = (ip: string) => { |
| 72 | cy.dataCy("dns-zones-button-add").click(); |
| 73 | cy.dataCy("add-dns-zone-modal").should("exist"); |
| 74 | cy.dataCy("modal-radio-reverse-zone-ip").click(); |
| 75 | cy.dataCy("modal-radio-reverse-zone-ip").should("be.checked"); |
| 76 | |
| 77 | fillReversedDnsZoneWithSkipOverlapCheck(ip); |
| 78 | |
| 79 | cy.dataCy("modal-button-add").click(); |
| 80 | cy.dataCy("add-dns-zone-modal").should("not.exist"); |
| 81 | }; |
| 82 | |
| 83 | const deleteDnsZone = (zoneName: string) => { |
| 84 | selectEntry(zoneName); |
| 85 | |
| 86 | cy.dataCy("dns-zones-button-delete").click(); |
| 87 | cy.dataCy("dns-zones-delete-modal").should("exist"); |
| 88 | |
| 89 | cy.dataCy("modal-button-ok").click(); |
| 90 | cy.dataCy("dns-zones-delete-modal").should("not.exist"); |
| 91 | }; |
| 92 | |
| 93 | const isDisabled = (name: string) => { |
| 94 | cy.get("tr[id='" + name + "'] td[data-label=idnszoneactive]").contains( |
| 95 | "Disabled" |
| 96 | ); |
| 97 | }; |
| 98 | |
| 99 | export const isEnabled = (name: string) => { |
| 100 | cy.get("tr[id='" + name + "'] td[data-label=idnszoneactive]").contains( |
| 101 | "Enabled" |
| 102 | ); |
| 103 | }; |
| 104 | |
| 105 | export const disableDnsZone = (zoneName: string) => { |
| 106 | selectEntry(zoneName); |
| 107 | |
| 108 | cy.dataCy("dns-zones-button-disable").click(); |
| 109 | cy.dataCy("dns-zones-enable-disable-modal").should("exist"); |
| 110 | cy.dataCy("modal-button-ok").click(); |
| 111 | cy.dataCy("dns-zones-enable-disable-modal").should("not.exist"); |
| 112 | |
| 113 | isDisabled(zoneName); |
| 114 | }; |
| 115 | |
| 116 | const enableDnsZone = (zoneName: string) => { |
| 117 | selectEntry(zoneName); |
| 118 | |
| 119 | cy.dataCy("dns-zones-button-enable").click(); |
| 120 | cy.dataCy("dns-zones-enable-disable-modal").should("exist"); |
| 121 | cy.dataCy("modal-button-ok").click(); |
| 122 | cy.dataCy("dns-zones-enable-disable-modal").should("not.exist"); |
| 123 | |
| 124 | isEnabled(zoneName); |
| 125 | }; |
| 126 | |
| 127 | // E.g. "my-dnszone" -> "my-dnszone." |
| 128 | export const parseZoneName = (zoneName: string) => { |
| 129 | return zoneName + "."; |
| 130 | }; |
| 131 | |
| 132 | When("I create a DNS zone {string}", (zoneName: string) => { |
| 133 | createDnsZone(zoneName); |
| 134 | }); |
| 135 | |
| 136 | When("I create a reverse DNS zone {string}", (ip: string) => { |
| 137 | createReversedDnsZone(ip); |
| 138 | }); |
| 139 | |
| 140 | Given("I delete DNS zone {string}", (zoneName: string) => { |
| 141 | loginAsAdmin(); |
| 142 | navigateTo("dns-zones"); |
| 143 | |
| 144 | deleteDnsZone(zoneName); |
| 145 | |
| 146 | searchForEntry(zoneName); |
| 147 | entryDoesNotExist(zoneName); |
| 148 | logout(); |
| 149 | }); |
| 150 | |
| 151 | When( |
| 152 | "I create a DNS zone {string} with skip overlap check", |
| 153 | (zoneName: string) => { |
| 154 | createDnsZoneWithSkipOverlapCheck(zoneName); |
| 155 | } |
| 156 | ); |
| 157 | |
| 158 | When( |
| 159 | "I create a reverse DNS zone {string} with skip overlap check", |
| 160 | (ip: string) => { |
| 161 | createReversedDnsZoneWithSkipOverlapCheck(ip); |
| 162 | } |
| 163 | ); |
| 164 | |
| 165 | Then("I should see DNS zone {string} in the list", (zoneName: string) => { |
| 166 | entryExists(zoneName); |
| 167 | }); |
| 168 | |
| 169 | Then("I should not see DNS zone {string} in the list", (zoneName: string) => { |
| 170 | searchForEntry(zoneName); |
| 171 | entryDoesNotExist(zoneName); |
| 172 | }); |
| 173 | |
| 174 | Given("DNS zone {string} exists", (zoneName: string) => { |
| 175 | loginAsAdmin(); |
| 176 | navigateTo("dns-zones"); |
| 177 | |
| 178 | createDnsZone(zoneName); |
| 179 | |
| 180 | searchForEntry(parseZoneName(zoneName)); |
| 181 | entryExists(parseZoneName(zoneName)); |
| 182 | logout(); |
| 183 | }); |
| 184 | |
| 185 | Then( |
| 186 | "I should see DNS zone {string} in the list disabled", |
| 187 | (zoneName: string) => { |
| 188 | isDisabled(zoneName); |
| 189 | } |
| 190 | ); |
| 191 | |
| 192 | Then( |
| 193 | "I should see DNS zone {string} in the list enabled", |
| 194 | (zoneName: string) => { |
| 195 | isEnabled(zoneName); |
| 196 | } |
| 197 | ); |
| 198 | |
| 199 | When("I cancel the creation of the {string} DNS zone", (zoneName: string) => { |
| 200 | cy.dataCy("dns-zones-button-add").click(); |
| 201 | cy.dataCy("add-dns-zone-modal").should("exist"); |
| 202 | |
| 203 | fillDnsZone(zoneName); |
| 204 | |
| 205 | cy.dataCy("modal-button-cancel").click(); |
| 206 | cy.dataCy("add-dns-zone-modal").should("not.exist"); |
| 207 | }); |
| 208 | |
| 209 | When("I disable DNS zone {string}", (zoneName: string) => { |
| 210 | disableDnsZone(zoneName); |
| 211 | }); |
| 212 | |
| 213 | When("I enable DNS zone {string}", (zoneName: string) => { |
| 214 | enableDnsZone(zoneName); |
| 215 | }); |
| 216 | |
| 217 | Then("I close the add DNS zone modal", () => { |
| 218 | cy.dataCy("modal-button-cancel").click(); |
| 219 | cy.dataCy("add-dns-zone-modal").should("not.exist"); |
| 220 | }); |
| 221 | |
| 222 | When( |
| 223 | "I create an invalid DNS reverse zone that triggers a validation error", |
| 224 | () => { |
| 225 | cy.dataCy("dns-zones-button-add").click(); |
| 226 | cy.dataCy("add-dns-zone-modal").should("exist"); |
| 227 | |
| 228 | cy.dataCy("modal-radio-reverse-zone-ip").click(); |
| 229 | cy.dataCy("modal-radio-reverse-zone-ip").should("be.checked"); |
| 230 | cy.dataCy("modal-textbox-reverse-zone-ip").type("192.168.1.xx"); |
| 231 | cy.dataCy("modal-textbox-reverse-zone-ip").should( |
| 232 | "have.value", |
| 233 | "192.168.1.xx" |
| 234 | ); |
| 235 | cy.dataCy("modal-textbox-reverse-zone-ip").should( |
| 236 | "have.attr", |
| 237 | "aria-invalid", |
| 238 | "true" |
| 239 | ); |
| 240 | |
| 241 | cy.dataCy("modal-button-add").click(); |
| 242 | cy.dataCy("add-dns-zone-modal").should("exist"); |
| 243 | cy.dataCy("modal-textbox-reverse-zone-ip").should( |
| 244 | "have.attr", |
| 245 | "aria-invalid", |
| 246 | "true" |
| 247 | ); |
| 248 | } |
| 249 | ); |