How to use matchTarget method in storybook-root

Best JavaScript code snippet using storybook-root

test-matching-rules.js

Source:test-matching-rules.js Github

copy

Full Screen

1/*2# This Source Code Form is subject to the terms of the Mozilla Public3# License, v. 2.0. If a copy of the MPL was not distributed with this4# file, You can obtain one at http://mozilla.org/MPL/2.0/.5*/6'use strict';7import * as Constants from '../common/constants.js';8import { MatchingRules } from '../common/matching-rules.js';9import * as RecipientParser from '../common/recipient-parser.js';10import { assert } from 'tiny-esm-test-runner';11const { is, ok, ng } = assert;12export async function test_load_and_export() {13 const matchingRules = new MatchingRules({14 baseRules: [15 { id: 'base',16 itemsLocal: ['base'] },17 { id: 'user',18 itemsLocal: ['base'] },19 ],20 userRules: [21 { id: 'user',22 itemsLocal: ['user'] },23 ],24 overrideRules: [25 { id: 'base',26 itemsFile: '/path/fo/base/overridden' },27 { id: 'override',28 itemsLocal: ['override'] },29 ],30 });31 is(32 [33 { id: 'base',34 name: '',35 enabled: true,36 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,37 highlight: Constants.HIGHLIGHT_NEVER,38 action: Constants.ACTION_NONE,39 itemsSource: Constants.SOURCE_LOCAL_CONFIG,40 itemsLocal: ['base'],41 itemsFile: '/path/fo/base/overridden',42 confirmTitle: '',43 confirmMessage: '',44 $lockedKeys: ['itemsFile'] },45 { id: 'user',46 name: '',47 enabled: true,48 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,49 highlight: Constants.HIGHLIGHT_NEVER,50 action: Constants.ACTION_NONE,51 itemsSource: Constants.SOURCE_LOCAL_CONFIG,52 itemsLocal: ['user'],53 itemsFile: '',54 confirmTitle: '',55 confirmMessage: '',56 $lockedKeys: [] },57 { id: 'override',58 name: '',59 enabled: true,60 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,61 highlight: Constants.HIGHLIGHT_NEVER,62 action: Constants.ACTION_NONE,63 itemsSource: Constants.SOURCE_LOCAL_CONFIG,64 itemsLocal: ['override'],65 itemsFile: '',66 confirmTitle: '',67 confirmMessage: '',68 $lockedKeys: ['itemsLocal'] },69 ],70 matchingRules.all71 );72}73export function test_add() {74 const matchingRules = new MatchingRules();75 matchingRules.add({ id: 'one' });76 matchingRules.add({ id: 'two' });77 matchingRules.add({ id: 'three' });78 is(['one', 'two', 'three'],79 matchingRules.all.map(rule => rule.id));80}81export function test_remove() {82 const matchingRules = new MatchingRules({83 base: [84 { id: 'one' },85 { id: 'two' },86 { id: 'three' },87 ],88 });89 matchingRules.remove('two');90 is(['one', 'three'],91 matchingRules.all.map(rule => rule.id));92}93export function test_moveUp() {94 const matchingRules = new MatchingRules({95 base: [96 { id: 'one' },97 { id: 'two' },98 { id: 'three' },99 ],100 });101 matchingRules.moveUp('three');102 is(['one', 'three', 'two'],103 matchingRules.all.map(rule => rule.id));104 matchingRules.moveUp('three');105 is(['three', 'one', 'two'],106 matchingRules.all.map(rule => rule.id));107 matchingRules.moveUp('three');108 is(['three', 'one', 'two'],109 matchingRules.all.map(rule => rule.id));110}111export function test_moveDown() {112 const matchingRules = new MatchingRules({113 base: [114 { id: 'one' },115 { id: 'two' },116 { id: 'three' },117 ],118 });119 matchingRules.moveDown('one');120 is(['two', 'one', 'three'],121 matchingRules.all.map(rule => rule.id));122 matchingRules.moveDown('one');123 is(['two', 'three', 'one'],124 matchingRules.all.map(rule => rule.id));125 matchingRules.moveDown('one');126 is(['two', 'three', 'one'],127 matchingRules.all.map(rule => rule.id));128}129export async function test_populate() {130 const matchingRules = new MatchingRules({131 base: [132 { id: 'local',133 itemsSource: Constants.SOURCE_LOCAL_CONFIG,134 itemsLocal: ['local'],135 itemsFile: '/path/to/local' },136 { id: 'file',137 itemsSource: Constants.SOURCE_FILE,138 itemsLocal: ['file'],139 itemsFile: '/path/to/file' },140 ],141 });142 const requestedFiles = [];143 await matchingRules.populate(async path => {144 requestedFiles.push(path);145 return 'file';146 });147 is(148 {149 local: ['local'],150 file: ['file'],151 requestedFiles: ['/path/to/file'],152 },153 {154 local: matchingRules.get('local').items,155 file: matchingRules.get('file').items,156 requestedFiles,157 }158 );159}160const NO_REACTION_RULES = [161 { id: 'nothing matched to recipient domain',162 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,163 highlight: Constants.HIGHLIGHT_NEVER,164 action: Constants.ACTION_NONE,165 itemsLocal: ['none.example.com', '@none.clear-code.com'] },166 { id: 'nothing matched to attachment name',167 matchTarget: Constants.MATCH_TO_ATTACHMENT_NAME,168 highlight: Constants.HIGHLIGHT_NEVER,169 action: Constants.ACTION_NONE,170 itemsLocal: ['nowhere'] },171 { id: 'nothing matched to attachment suffix',172 matchTarget: Constants.MATCH_TO_ATTACHMENT_SUFFIX,173 highlight: Constants.HIGHLIGHT_NEVER,174 action: Constants.ACTION_NONE,175 itemsLocal: ['none-without-dot', '.none-with-dot'] },176 { id: 'nothing matched to subject',177 matchTarget: Constants.MATCH_TO_SUBJECT,178 highlight: Constants.HIGHLIGHT_NEVER,179 action: Constants.ACTION_NONE,180 itemsLocal: ['never'] },181 { id: 'nothing matched to body',182 matchTarget: Constants.MATCH_TO_BODY,183 highlight: Constants.HIGHLIGHT_NEVER,184 action: Constants.ACTION_NONE,185 itemsLocal: ['never'] },186 { id: 'nothing matched to subject and body',187 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,188 highlight: Constants.HIGHLIGHT_NEVER,189 action: Constants.ACTION_NONE,190 itemsLocal: ['never'] },191];192const DISABLED_RULES = [193 { id: 'nothing matched to recipient domain',194 enabled: false,195 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,196 highlight: Constants.HIGHLIGHT_ALWAYS,197 action: Constants.ACTION_BLOCK_ALWAYS,198 itemsLocal: ['none.example.com', '@none.clear-code.com'] },199 { id: 'nothing matched to attachment name',200 enabled: false,201 matchTarget: Constants.MATCH_TO_ATTACHMENT_NAME,202 highlight: Constants.HIGHLIGHT_ALWAYS,203 action: Constants.ACTION_BLOCK_ALWAYS,204 itemsLocal: ['nowhere'] },205 { id: 'nothing matched to attachment suffix',206 enabled: false,207 matchTarget: Constants.MATCH_TO_ATTACHMENT_SUFFIX,208 highlight: Constants.HIGHLIGHT_ALWAYS,209 action: Constants.ACTION_BLOCK_ALWAYS,210 itemsLocal: ['none-without-dot', '.none-with-dot'] },211 { id: 'nothing matched to subject',212 enabled: false,213 matchTarget: Constants.MATCH_TO_SUBJECT,214 highlight: Constants.HIGHLIGHT_ALWAYS,215 action: Constants.ACTION_BLOCK_ALWAYS,216 itemsLocal: ['never'] },217 { id: 'nothing matched to body',218 enabled: false,219 matchTarget: Constants.MATCH_TO_BODY,220 highlight: Constants.HIGHLIGHT_ALWAYS,221 action: Constants.ACTION_BLOCK_ALWAYS,222 itemsLocal: ['never'] },223 { id: 'nothing matched to subject and body',224 enabled: false,225 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,226 highlight: Constants.HIGHLIGHT_ALWAYS,227 action: Constants.ACTION_BLOCK_ALWAYS,228 itemsLocal: ['never'] },229];230const RECIPIENT_DOMAIN_RULES = [231 { id: 'highlighted by recipient domain always',232 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,233 highlight: Constants.HIGHLIGHT_ALWAYS,234 itemsLocal: ['highlighted-always.example.com', '@highlighted-always.clear-code.com'] },235 { id: 'highlighted by recipient domain only with attachment',236 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,237 highlight: Constants.HIGHLIGHT_ONLY_WITH_ATTACHMENTS,238 itemsLocal: ['highlighted-attachment.example.com', '@highlighted-attachment.clear-code.com'] },239 { id: 'highlighted by recipient domain only external',240 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,241 highlight: Constants.HIGHLIGHT_ONLY_EXTERNALS,242 itemsLocal: ['highlighted-external.example.com', '@highlighted-external.clear-code.com'] },243 { id: 'highlighted by recipient domain only external with attachment',244 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,245 highlight: Constants.HIGHLIGHT_ONLY_EXTERNALS_WITH_ATTACHMENTS,246 itemsLocal: ['highlighted-external-attachment.example.com', '@highlighted-external-attachment.clear-code.com'] },247 { id: 'reconfirmed by recipient domain always',248 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,249 action: Constants.ACTION_RECONFIRM_ALWAYS,250 itemsLocal: ['reconfirmed-always.example.com', '@reconfirmed-always.clear-code.com'] },251 { id: 'reconfirmed by recipient domain only with attachment',252 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,253 action: Constants.ACTION_RECONFIRM_ONLY_WITH_ATTACHMENTS,254 itemsLocal: ['reconfirmed-attachment.example.com', '@reconfirmed-attachment.clear-code.com'] },255 { id: 'reconfirmed by recipient domain only external',256 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,257 action: Constants.ACTION_RECONFIRM_ONLY_EXTERNALS,258 itemsLocal: ['reconfirmed-external.example.com', '@reconfirmed-external.clear-code.com'] },259 { id: 'reconfirmed by recipient domain only external with attachment',260 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,261 action: Constants.ACTION_RECONFIRM_ONLY_EXTERNALS_WITH_ATTACHMENTS,262 itemsLocal: ['reconfirmed-external-attachment.example.com', '@reconfirmed-external-attachment.clear-code.com'] },263 { id: 'blocked by recipient domain always',264 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,265 action: Constants.ACTION_BLOCK_ALWAYS,266 itemsLocal: ['blocked-always.example.com', '@blocked-always.clear-code.com'] },267 { id: 'blocked by recipient domain only with attachment',268 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,269 action: Constants.ACTION_BLOCK_ONLY_WITH_ATTACHMENTS,270 itemsLocal: ['blocked-attachment.example.com', '@blocked-attachment.clear-code.com'] },271 { id: 'blocked by recipient domain only external',272 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,273 action: Constants.ACTION_BLOCK_ONLY_EXTERNALS,274 itemsLocal: ['blocked-external.example.com', '@blocked-external.clear-code.com'] },275 { id: 'blocked by recipient domain only external with attachment',276 matchTarget: Constants.MATCH_TO_RECIPIENT_DOMAIN,277 action: Constants.ACTION_BLOCK_ONLY_EXTERNALS_WITH_ATTACHMENTS,278 itemsLocal: ['blocked-external-attachment.example.com', '@blocked-external-attachment.clear-code.com'] },279];280const ATTACHMENT_NAME_RULES = [281 { id: 'highlighted by attachment name',282 matchTarget: Constants.MATCH_TO_ATTACHMENT_NAME,283 highlight: Constants.HIGHLIGHT_ALWAYS,284 itemsLocal: ['highlighted-name'] },285 { id: 'highlighted by attachment name only external',286 matchTarget: Constants.MATCH_TO_ATTACHMENT_NAME,287 highlight: Constants.HIGHLIGHT_ONLY_EXTERNALS,288 itemsLocal: ['highlighted-external-name'] },289 { id: 'reconfirmed by attachment name',290 matchTarget: Constants.MATCH_TO_ATTACHMENT_NAME,291 action: Constants.ACTION_RECONFIRM_ALWAYS,292 itemsLocal: ['reconfirmed-name'] },293 { id: 'reconfirmed by attachment name only external',294 matchTarget: Constants.MATCH_TO_ATTACHMENT_NAME,295 action: Constants.ACTION_RECONFIRM_ONLY_EXTERNALS,296 itemsLocal: ['reconfirmed-external-name'] },297 { id: 'blocked by attachment name',298 matchTarget: Constants.MATCH_TO_ATTACHMENT_NAME,299 action: Constants.ACTION_BLOCK_ALWAYS,300 itemsLocal: ['blocked-name'] },301 { id: 'blocked by attachment name only external',302 matchTarget: Constants.MATCH_TO_ATTACHMENT_NAME,303 action: Constants.ACTION_BLOCK_ONLY_EXTERNALS,304 itemsLocal: ['blocked-external-name'] },305];306const ATTACHMENT_SUFFIX_RULES = [307 { id: 'highlighted by attachment suffix',308 matchTarget: Constants.MATCH_TO_ATTACHMENT_SUFFIX,309 highlight: Constants.HIGHLIGHT_ALWAYS,310 itemsLocal: ['highlighted-ext'] },311 { id: 'highlighted by attachment suffix only external',312 matchTarget: Constants.MATCH_TO_ATTACHMENT_SUFFIX,313 highlight: Constants.HIGHLIGHT_ONLY_EXTERNALS,314 itemsLocal: ['highlighted-external-ext'] },315 { id: 'reconfirmed by attachment suffix',316 matchTarget: Constants.MATCH_TO_ATTACHMENT_SUFFIX,317 action: Constants.ACTION_RECONFIRM_ALWAYS,318 itemsLocal: ['reconfirmed-ext'] },319 { id: 'reconfirmed by attachment suffix only external',320 matchTarget: Constants.MATCH_TO_ATTACHMENT_SUFFIX,321 action: Constants.ACTION_RECONFIRM_ONLY_EXTERNALS,322 itemsLocal: ['reconfirmed-external-ext'] },323 { id: 'blocked by attachment suffix',324 matchTarget: Constants.MATCH_TO_ATTACHMENT_SUFFIX,325 action: Constants.ACTION_BLOCK_ALWAYS,326 itemsLocal: ['.blocked-ext'] },327 { id: 'blocked by attachment suffix only external',328 matchTarget: Constants.MATCH_TO_ATTACHMENT_SUFFIX,329 action: Constants.ACTION_BLOCK_ONLY_EXTERNALS,330 itemsLocal: ['.blocked-external-ext'] },331];332const SUBJECT_RULES = [333 { id: 'highlighted by subject always',334 matchTarget: Constants.MATCH_TO_SUBJECT,335 highlight: Constants.HIGHLIGHT_ALWAYS,336 itemsLocal: ['highlight-always'] },337 { id: 'highlighted by subject only with attachment',338 matchTarget: Constants.MATCH_TO_SUBJECT,339 highlight: Constants.HIGHLIGHT_ONLY_WITH_ATTACHMENTS,340 itemsLocal: ['highlighted-attachment'] },341 { id: 'highlighted by subject only external',342 matchTarget: Constants.MATCH_TO_SUBJECT,343 highlight: Constants.HIGHLIGHT_ONLY_EXTERNALS,344 itemsLocal: ['highlighted-external'] },345 { id: 'highlighted by subject only external with attachment',346 matchTarget: Constants.MATCH_TO_SUBJECT,347 highlight: Constants.HIGHLIGHT_ONLY_EXTERNALS_WITH_ATTACHMENTS,348 itemsLocal: ['highlighted-both-external-attachment'] },349 { id: 'reconfirmed by subject always',350 matchTarget: Constants.MATCH_TO_SUBJECT,351 action: Constants.ACTION_RECONFIRM_ALWAYS,352 itemsLocal: ['reconfirmed-always'] },353 { id: 'reconfirmed by subject only with attachment',354 matchTarget: Constants.MATCH_TO_SUBJECT,355 action: Constants.ACTION_RECONFIRM_ONLY_WITH_ATTACHMENTS,356 itemsLocal: ['reconfirmed-attachment'] },357 { id: 'reconfirmed by subject only external',358 matchTarget: Constants.MATCH_TO_SUBJECT,359 action: Constants.ACTION_RECONFIRM_ONLY_EXTERNALS,360 itemsLocal: ['reconfirmed-external'] },361 { id: 'reconfirmed by subject only external with attachment',362 matchTarget: Constants.MATCH_TO_SUBJECT,363 action: Constants.ACTION_RECONFIRM_ONLY_EXTERNALS_WITH_ATTACHMENTS,364 itemsLocal: ['reconfirmed-both-external-attachment'] },365 { id: 'blocked by subject always',366 matchTarget: Constants.MATCH_TO_SUBJECT,367 action: Constants.ACTION_BLOCK_ALWAYS,368 itemsLocal: ['blocked-always'] },369 { id: 'blocked by subject only with attachment',370 matchTarget: Constants.MATCH_TO_SUBJECT,371 action: Constants.ACTION_BLOCK_ONLY_WITH_ATTACHMENTS,372 itemsLocal: ['blocked-attachment'] },373 { id: 'blocked by subject only external',374 matchTarget: Constants.MATCH_TO_SUBJECT,375 action: Constants.ACTION_BLOCK_ONLY_EXTERNALS,376 itemsLocal: ['blocked-external'] },377 { id: 'blocked by subject only external with attachment',378 matchTarget: Constants.MATCH_TO_SUBJECT,379 action: Constants.ACTION_BLOCK_ONLY_EXTERNALS_WITH_ATTACHMENTS,380 itemsLocal: ['blocked-both-external-attachment'] },381];382const BODY_RULES = [383 { id: 'highlighted by body always',384 matchTarget: Constants.MATCH_TO_BODY,385 highlight: Constants.HIGHLIGHT_ALWAYS,386 itemsLocal: ['highlight-always'] },387 { id: 'highlighted by body only with attachment',388 matchTarget: Constants.MATCH_TO_BODY,389 highlight: Constants.HIGHLIGHT_ONLY_WITH_ATTACHMENTS,390 itemsLocal: ['highlighted-attachment'] },391 { id: 'highlighted by body only external',392 matchTarget: Constants.MATCH_TO_BODY,393 highlight: Constants.HIGHLIGHT_ONLY_EXTERNALS,394 itemsLocal: ['highlighted-external'] },395 { id: 'highlighted by body only external with attachment',396 matchTarget: Constants.MATCH_TO_BODY,397 highlight: Constants.HIGHLIGHT_ONLY_EXTERNALS_WITH_ATTACHMENTS,398 itemsLocal: ['highlighted-both-external-attachment'] },399 { id: 'reconfirmed by body always',400 matchTarget: Constants.MATCH_TO_BODY,401 action: Constants.ACTION_RECONFIRM_ALWAYS,402 itemsLocal: ['reconfirmed-always'] },403 { id: 'reconfirmed by body only with attachment',404 matchTarget: Constants.MATCH_TO_BODY,405 action: Constants.ACTION_RECONFIRM_ONLY_WITH_ATTACHMENTS,406 itemsLocal: ['reconfirmed-attachment'] },407 { id: 'reconfirmed by body only external',408 matchTarget: Constants.MATCH_TO_BODY,409 action: Constants.ACTION_RECONFIRM_ONLY_EXTERNALS,410 itemsLocal: ['reconfirmed-external'] },411 { id: 'reconfirmed by body only external with attachment',412 matchTarget: Constants.MATCH_TO_BODY,413 action: Constants.ACTION_RECONFIRM_ONLY_EXTERNALS_WITH_ATTACHMENTS,414 itemsLocal: ['reconfirmed-both-external-attachment'] },415 { id: 'blocked by body always',416 matchTarget: Constants.MATCH_TO_BODY,417 action: Constants.ACTION_BLOCK_ALWAYS,418 itemsLocal: ['blocked-always'] },419 { id: 'blocked by body only with attachment',420 matchTarget: Constants.MATCH_TO_BODY,421 action: Constants.ACTION_BLOCK_ONLY_WITH_ATTACHMENTS,422 itemsLocal: ['blocked-attachment'] },423 { id: 'blocked by body only external',424 matchTarget: Constants.MATCH_TO_BODY,425 action: Constants.ACTION_BLOCK_ONLY_EXTERNALS,426 itemsLocal: ['blocked-external'] },427 { id: 'blocked by body only external with attachment',428 matchTarget: Constants.MATCH_TO_BODY,429 action: Constants.ACTION_BLOCK_ONLY_EXTERNALS_WITH_ATTACHMENTS,430 itemsLocal: ['blocked-both-external-attachment'] },431];432const SUBJECT_OR_BODY_RULES = [433 { id: 'highlighted by subject or body always',434 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,435 highlight: Constants.HIGHLIGHT_ALWAYS,436 itemsLocal: ['highlight-always'] },437 { id: 'highlighted by subject or body only with attachment',438 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,439 highlight: Constants.HIGHLIGHT_ONLY_WITH_ATTACHMENTS,440 itemsLocal: ['highlighted-attachment'] },441 { id: 'highlighted by subject or body only external',442 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,443 highlight: Constants.HIGHLIGHT_ONLY_EXTERNALS,444 itemsLocal: ['highlighted-external'] },445 { id: 'highlighted by subject or body only external with attachment',446 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,447 highlight: Constants.HIGHLIGHT_ONLY_EXTERNALS_WITH_ATTACHMENTS,448 itemsLocal: ['highlighted-both-external-attachment'] },449 { id: 'reconfirmed by subject or body always',450 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,451 action: Constants.ACTION_RECONFIRM_ALWAYS,452 itemsLocal: ['reconfirmed-always'] },453 { id: 'reconfirmed by subject or body only with attachment',454 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,455 action: Constants.ACTION_RECONFIRM_ONLY_WITH_ATTACHMENTS,456 itemsLocal: ['reconfirmed-attachment'] },457 { id: 'reconfirmed by subject or body only external',458 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,459 action: Constants.ACTION_RECONFIRM_ONLY_EXTERNALS,460 itemsLocal: ['reconfirmed-external'] },461 { id: 'reconfirmed by subject or body only external with attachment',462 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,463 action: Constants.ACTION_RECONFIRM_ONLY_EXTERNALS_WITH_ATTACHMENTS,464 itemsLocal: ['reconfirmed-both-external-attachment'] },465 { id: 'blocked by subject or body always',466 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,467 action: Constants.ACTION_BLOCK_ALWAYS,468 itemsLocal: ['blocked-always'] },469 { id: 'blocked by subject or body only with attachment',470 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,471 action: Constants.ACTION_BLOCK_ONLY_WITH_ATTACHMENTS,472 itemsLocal: ['blocked-attachment'] },473 { id: 'blocked by subject or body only external',474 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,475 action: Constants.ACTION_BLOCK_ONLY_EXTERNALS,476 itemsLocal: ['blocked-external'] },477 { id: 'blocked by subject or body only external with attachment',478 matchTarget: Constants.MATCH_TO_SUBJECT_OR_BODY,479 action: Constants.ACTION_BLOCK_ONLY_EXTERNALS_WITH_ATTACHMENTS,480 itemsLocal: ['blocked-both-external-attachment'] },481];482const RULES = [483 ...NO_REACTION_RULES,484 ...DISABLED_RULES,485 ...RECIPIENT_DOMAIN_RULES,486 ...ATTACHMENT_NAME_RULES,487 ...ATTACHMENT_SUFFIX_RULES,488 ...SUBJECT_RULES,489 ...BODY_RULES,490 ...SUBJECT_OR_BODY_RULES,491];492const RECONFIRM_ACTIONS = new Set([493 Constants.ACTION_RECONFIRM_ALWAYS,494 Constants.ACTION_RECONFIRM_ONLY_EXTERNALS,495 Constants.ACTION_RECONFIRM_ONLY_WITH_ATTACHMENTS,496 Constants.ACTION_RECONFIRM_ONLY_EXTERNALS_WITH_ATTACHMENTS,497]);498const RECONFIRM_RULES = [499 ...RECIPIENT_DOMAIN_RULES,500 ...ATTACHMENT_NAME_RULES,501 ...ATTACHMENT_SUFFIX_RULES,502 ...SUBJECT_RULES,503 ...BODY_RULES,504 ...SUBJECT_OR_BODY_RULES,505].filter(rule => RECONFIRM_ACTIONS.has(rule.action));506const BLOCK_ACTIONS = new Set([507 Constants.ACTION_BLOCK_ALWAYS,508 Constants.ACTION_BLOCK_ONLY_EXTERNALS,509 Constants.ACTION_BLOCK_ONLY_WITH_ATTACHMENTS,510 Constants.ACTION_BLOCK_ONLY_EXTERNALS_WITH_ATTACHMENTS,511]);512const BLOCK_RULES = [513 ...RECIPIENT_DOMAIN_RULES,514 ...ATTACHMENT_NAME_RULES,515 ...ATTACHMENT_SUFFIX_RULES,516 ...SUBJECT_RULES,517 ...BODY_RULES,518 ...SUBJECT_OR_BODY_RULES,519].filter(rule => BLOCK_ACTIONS.has(rule.action));520const RECIPIENTS_HIGHLIGHTED_ALWAYS = [521 'lowercase@highlighted-always.example.com',522 'uppercase@HIGHLIGHTED-ALWAYS.CLEAR-CODE.COM',523 'mixedcase@HiGhLiGhTeD-aLwAyS.ExAmPlE.cOm',524];525const RECIPIENTS_HIGHLIGHTED_WITH_ATTACHMENTS = [526 'lowercase@highlighted-attachment.example.com',527 'uppercase@HIGHLIGHTED-ATTACHMENT.CLEAR-CODE.COM',528 'mixedcase@HiGhLiGhTeD-aTtAcHmEnT.ExAmPlE.cOm',529];530const RECIPIENTS_HIGHLIGHTED_EXTERNALS = [531 'lowercase@highlighted-external.example.com',532 'uppercase@HIGHLIGHTED-EXTERNAL.CLEAR-CODE.COM',533 'mixedcase@HiGhLiGhTeD-eXtErNaL.ExAmPlE.cOm',534];535const RECIPIENTS_HIGHLIGHTED_EXTERNALS_WITH_ATTACHMENTS = [536 'lowercase@highlighted-external-attachment.example.com',537 'uppercase@HIGHLIGHTED-EXTERNAL-ATTACHMENT.CLEAR-CODE.COM',538 'mixedcase@HiGhLiGhTeD-eXtErNaL-aTtAcHmEnT.ExAmPlE.cOm',539];540const RECIPIENTS_RECONFIRMED_ALWAYS = [541 'lowercase@reconfirmed-always.example.com',542 'uppercase@RECONFIRMED-ALWAYS.CLEAR-CODE.COM',543 'mixedcase@ReCoNfIrMeD-aLwAyS.ExAmPlE.cOm',544];545const RECIPIENTS_RECONFIRMED_WITH_ATTACHMENTS = [546 'lowercase@reconfirmed-attachment.example.com',547 'uppercase@RECONFIRMED-ATTACHMENT.CLEAR-CODE.COM',548 'mixedcase@ReCoNfIrMeD-aTtAcHmEnT.ExAmPlE.cOm',549];550const RECIPIENTS_RECONFIRMED_EXTERNALS = [551 'lowercase@reconfirmed-external.example.com',552 'uppercase@RECONFIRMED-EXTERNAL.CLEAR-CODE.COM',553 'mixedcase@ReCoNfIrMeD-eXtErNaL.ExAmPlE.cOm',554];555const RECIPIENTS_RECONFIRMED_EXTERNALS_WITH_ATTACHMENTS = [556 'lowercase@reconfirmed-external-attachment.example.com',557 'uppercase@RECONFIRMED-EXTERNAL-ATTACHMENT.CLEAR-CODE.COM',558 'mixedcase@ReCoNfIrMeD-eXtErNaL-aTtAcHmEnT.ExAmPlE.cOm',559];560const RECIPIENTS_BLOCKED_ALWAYS = [561 'lowercase@blocked-always.example.com',562 'uppercase@BLOCKED-ALWAYS.CLEAR-CODE.COM',563 'mixedcase@BlOcKeD-aLwAyS.ExAmPlE.cOm',564];565const RECIPIENTS_BLOCKED_WITH_ATTACHMENTS = [566 'lowercase@blocked-attachment.example.com',567 'uppercase@BLOCKED-ATTACHMENT.CLEAR-CODE.COM',568 'mixedcase@BlOcKeD-aTtAcHmEnT.ExAmPlE.cOm',569];570const RECIPIENTS_BLOCKED_EXTERNALS = [571 'lowercase@blocked-external.example.com',572 'uppercase@BLOCKED-EXTERNAL.CLEAR-CODE.COM',573 'mixedcase@BlOcKeD-eXtErNaL.ExAmPlE.cOm',574];575const RECIPIENTS_BLOCKED_EXTERNALS_WITH_ATTACHMENTS = [576 'lowercase@blocked-external-attachment.example.com',577 'uppercase@BLOCKED-EXTERNAL-ATTACHMENT.CLEAR-CODE.COM',578 'mixedcase@BlOcKeD-eXtErNaL-aTtAcHmEnT.ExAmPlE.cOm',579];580const RECIPIENTS = [581 'none@example.com',582 'none@clear-code.com',583 'lowercase@none.example.com',584 'uppercase@NONE.CLEAR-CODE.COM',585 'mixedcase@NoNe.ExAmPlE.cOm',586 ...RECIPIENTS_HIGHLIGHTED_ALWAYS,587 ...RECIPIENTS_HIGHLIGHTED_WITH_ATTACHMENTS,588 ...RECIPIENTS_HIGHLIGHTED_EXTERNALS,589 ...RECIPIENTS_HIGHLIGHTED_EXTERNALS_WITH_ATTACHMENTS,590 ...RECIPIENTS_RECONFIRMED_ALWAYS,591 ...RECIPIENTS_RECONFIRMED_WITH_ATTACHMENTS,592 ...RECIPIENTS_RECONFIRMED_EXTERNALS,593 ...RECIPIENTS_RECONFIRMED_EXTERNALS_WITH_ATTACHMENTS,594 ...RECIPIENTS_BLOCKED_ALWAYS,595 ...RECIPIENTS_BLOCKED_WITH_ATTACHMENTS,596 ...RECIPIENTS_BLOCKED_EXTERNALS,597 ...RECIPIENTS_BLOCKED_EXTERNALS_WITH_ATTACHMENTS,598 'address-like@highlighted-always.clear-code.com <address-like@exmaple.com>',599 'address-like@reconfirm-alwaysc.lear-code.com <address-like@example.org>',600 'address-like@blocked-always.com <address-like@clear-code.com>',601 'address-like@example.org <address-like@highlighted-always.CLEAR-code.com>',602 'address-like@exmaple.com <address-like@reconfirmed-always.CLEAR-code.com>',603 'address-like@clear-code.com <address-like@blocked-always.example.com>',604].map(RecipientParser.parse);605const ATTACHMENTS_HIGHLIGHTED_NAME = [606 'HiGhLiGhTeD-name-start',607 'middle-hIgHlIgHtEd-name-middle',608 'end-HIGHlighted-name',609];610const ATTACHMENTS_HIGHLIGHTED_NAME_EXTERNALS = [611 'HiGhLiGhTeD-external-name-start',612 'middle-hIgHlIgHtEd-external-name-middle',613 'end-HIGHlighted-external-name',614];615const ATTACHMENTS_RECONFIRMED_NAME = [616 'ReCoNfIrMeD-name-attachment-start',617 'middle-rEcOnFiRmEd-name-middle',618 'end-REconfirmed-name',619];620const ATTACHMENTS_RECONFIRMED_NAME_EXTERNALS = [621 'ReCoNfIrMeD-external-name-attachment-start',622 'middle-rEcOnFiRmEd-external-name-middle',623 'end-REconfirmed-external-name',624];625const ATTACHMENTS_BLOCKED_NAME = [626 'bLoCkEd-name-start',627 'middle-BlOcKeD-name-middle',628 'end-blockED-name',629];630const ATTACHMENTS_BLOCKED_NAME_EXTERNALS = [631 'bLoCkEd-external-name-start',632 'middle-BlOcKeD-external-name-middle',633 'end-blockED-external-name',634];635const ATTACHMENTS_HIGHLIGHTED_SUFFIX = [636 'basename.HiGhLiGhTeD-ext',637];638const ATTACHMENTS_HIGHLIGHTED_SUFFIX_EXTERNALS = [639 'basename.HiGhLiGhTeD-external-ext',640];641const ATTACHMENTS_RECONFIRMED_SUFFIX = [642 'basename.rEcOnFiRmEd-ext',643];644const ATTACHMENTS_RECONFIRMED_SUFFIX_EXTERNALS = [645 'basename.rEcOnFiRmEd-external-ext',646];647const ATTACHMENTS_BLOCKED_SUFFIX = [648 'basename.blockED-ext',649];650const ATTACHMENTS_BLOCKED_SUFFIX_EXTERNALS = [651 'basename.blockED-external-ext',652];653const ATTACHMENTS = [654 'not-contain',655 'nOwHeRe-start',656 'middle-NoWhErE-middle',657 'end-NOwhere',658 ...ATTACHMENTS_HIGHLIGHTED_NAME,659 ...ATTACHMENTS_HIGHLIGHTED_NAME_EXTERNALS,660 ...ATTACHMENTS_RECONFIRMED_NAME,661 ...ATTACHMENTS_RECONFIRMED_NAME_EXTERNALS,662 ...ATTACHMENTS_BLOCKED_NAME,663 ...ATTACHMENTS_BLOCKED_NAME_EXTERNALS,664 ...ATTACHMENTS_HIGHLIGHTED_SUFFIX,665 'hIgHlIgHtEd-ext.ext',666 'basename.HIGHlighted-ext.ext',667 ...ATTACHMENTS_HIGHLIGHTED_SUFFIX_EXTERNALS,668 ...ATTACHMENTS_RECONFIRMED_SUFFIX,669 'ReCoNfIrMeD-ext.ext',670 'REconfirmed-ext.ext',671 ...ATTACHMENTS_RECONFIRMED_SUFFIX_EXTERNALS,672 ...ATTACHMENTS_BLOCKED_SUFFIX,673 'basename.bLoCkEd-ext-start.ext',674 'BlOcKeD-ext-middle.ext',675 ...ATTACHMENTS_BLOCKED_SUFFIX_EXTERNALS,676].map(attachment => ({ name: attachment }));677function recipientsToAddresses(classified) {678 return Object.fromEntries(679 Object.entries(classified)680 .map(([id, recipients]) => [id, recipients.map(recipient => recipient.address)])681 );682}683function attachmentsToNames(classified) {684 return Object.fromEntries(685 Object.entries(classified)686 .map(([id, attachments]) => [id, attachments.map(attachment => attachment.name)])687 );688}689export async function test_classifyRecipients() {690 const matchingRules = new MatchingRules({ base: RULES });691 await matchingRules.populate();692 is(693 [694 ...RECIPIENTS_HIGHLIGHTED_ALWAYS,695 'address-like@highlighted-always.CLEAR-code.com',696 ...RECIPIENTS_HIGHLIGHTED_EXTERNALS,697 ],698 [...matchingRules.getHighlightedRecipientAddresses({699 externals: RECIPIENTS,700 attachments: [],701 })]702 );703 is(704 [705 ...RECIPIENTS_HIGHLIGHTED_ALWAYS,706 'address-like@highlighted-always.CLEAR-code.com',707 ...RECIPIENTS_HIGHLIGHTED_WITH_ATTACHMENTS,708 ...RECIPIENTS_HIGHLIGHTED_EXTERNALS,709 ...RECIPIENTS_HIGHLIGHTED_EXTERNALS_WITH_ATTACHMENTS,710 ],711 [...matchingRules.getHighlightedRecipientAddresses({712 externals: RECIPIENTS,713 attachments: ATTACHMENTS,714 })]715 );716 is(717 {718 'reconfirmed by recipient domain always': [719 ...RECIPIENTS_RECONFIRMED_ALWAYS,720 'address-like@reconfirmed-always.CLEAR-code.com',721 ],722 'reconfirmed by recipient domain only external': RECIPIENTS_RECONFIRMED_EXTERNALS,723 },724 recipientsToAddresses(matchingRules.classifyReconfirmRecipients({725 externals: RECIPIENTS,726 attachments: [],727 }))728 );729 is(730 {731 'reconfirmed by recipient domain always': [732 ...RECIPIENTS_RECONFIRMED_ALWAYS,733 'address-like@reconfirmed-always.CLEAR-code.com',734 ],735 'reconfirmed by recipient domain only with attachment': RECIPIENTS_RECONFIRMED_WITH_ATTACHMENTS,736 'reconfirmed by recipient domain only external': RECIPIENTS_RECONFIRMED_EXTERNALS,737 'reconfirmed by recipient domain only external with attachment': RECIPIENTS_RECONFIRMED_EXTERNALS_WITH_ATTACHMENTS,738 },739 recipientsToAddresses(matchingRules.classifyReconfirmRecipients({740 externals: RECIPIENTS,741 attachments: ATTACHMENTS,742 }))743 );744 is(745 {746 'blocked by recipient domain always': [747 ...RECIPIENTS_BLOCKED_ALWAYS,748 'address-like@blocked-always.example.com',749 ],750 'blocked by recipient domain only external': RECIPIENTS_BLOCKED_EXTERNALS,751 },752 recipientsToAddresses(matchingRules.classifyBlockRecipients({753 externals: RECIPIENTS,754 attachments: [],755 }))756 );757 is(758 {759 'blocked by recipient domain always': [760 ...RECIPIENTS_BLOCKED_ALWAYS,761 'address-like@blocked-always.example.com',762 ],763 'blocked by recipient domain only with attachment': RECIPIENTS_BLOCKED_WITH_ATTACHMENTS,764 'blocked by recipient domain only external': RECIPIENTS_BLOCKED_EXTERNALS,765 'blocked by recipient domain only external with attachment': RECIPIENTS_BLOCKED_EXTERNALS_WITH_ATTACHMENTS,766 },767 recipientsToAddresses(matchingRules.classifyBlockRecipients({768 externals: RECIPIENTS,769 attachments: ATTACHMENTS,770 }))771 );772}773export async function test_classifyAttachments() {774 const matchingRules = new MatchingRules({ base: RULES });775 await matchingRules.populate();776 is(777 [778 ...ATTACHMENTS_HIGHLIGHTED_NAME,779 ...ATTACHMENTS_HIGHLIGHTED_SUFFIX,780 ],781 [...matchingRules.getHighlightedAttachmentNames({782 attachments: ATTACHMENTS,783 hasExternal: false,784 })]785 );786 is(787 [788 ...ATTACHMENTS_HIGHLIGHTED_NAME,789 ...ATTACHMENTS_HIGHLIGHTED_NAME_EXTERNALS,790 ...ATTACHMENTS_HIGHLIGHTED_SUFFIX,791 ...ATTACHMENTS_HIGHLIGHTED_SUFFIX_EXTERNALS,792 ],793 [...matchingRules.getHighlightedAttachmentNames({794 attachments: ATTACHMENTS,795 hasExternal: true,796 })]797 );798 is(799 {800 'reconfirmed by attachment name': [801 ...ATTACHMENTS_RECONFIRMED_NAME,802 ],803 'reconfirmed by attachment suffix': [804 ...ATTACHMENTS_RECONFIRMED_SUFFIX,805 ],806 },807 attachmentsToNames(matchingRules.classifyReconfirmAttachments({808 attachments: ATTACHMENTS,809 hasExternal: false,810 }))811 );812 is(813 {814 'reconfirmed by attachment name': [815 ...ATTACHMENTS_RECONFIRMED_NAME,816 ],817 'reconfirmed by attachment name only external': [818 ...ATTACHMENTS_RECONFIRMED_NAME_EXTERNALS,819 ],820 'reconfirmed by attachment suffix': [821 ...ATTACHMENTS_RECONFIRMED_SUFFIX,822 ],823 'reconfirmed by attachment suffix only external': [824 ...ATTACHMENTS_RECONFIRMED_SUFFIX_EXTERNALS,825 ],826 },827 attachmentsToNames(matchingRules.classifyReconfirmAttachments({828 attachments: ATTACHMENTS,829 hasExternal: true,830 }))831 );832 is(833 {834 'blocked by attachment name': [835 ...ATTACHMENTS_BLOCKED_NAME,836 ],837 'blocked by attachment suffix': [838 ...ATTACHMENTS_BLOCKED_SUFFIX,839 ],840 },841 attachmentsToNames(matchingRules.classifyBlockAttachments({842 attachments: ATTACHMENTS,843 hasExternal: false,844 }))845 );846 is(847 {848 'blocked by attachment name': [849 ...ATTACHMENTS_BLOCKED_NAME,850 ],851 'blocked by attachment name only external': [852 ...ATTACHMENTS_BLOCKED_NAME_EXTERNALS,853 ],854 'blocked by attachment suffix': [855 ...ATTACHMENTS_BLOCKED_SUFFIX,856 ],857 'blocked by attachment suffix only external': [858 ...ATTACHMENTS_BLOCKED_SUFFIX_EXTERNALS,859 ],860 },861 attachmentsToNames(matchingRules.classifyBlockAttachments({862 attachments: ATTACHMENTS,863 hasExternal: true,864 }))865 );866}867export async function test_tryReconfirm_confirmed() {868 const matchingRules = new MatchingRules({ base: RULES });869 await matchingRules.populate();870 let confirmationCount = 0;871 const confirmed = await matchingRules.tryReconfirm({872 externals: RECIPIENTS,873 attachments: ATTACHMENTS,874 subject: 'reconfirmed-always reconfirmed-attachment reconfirmed-external reconfirmed-both-external-attachment',875 body: 'reconfirmed-always reconfirmed-attachment reconfirmed-external reconfirmed-both-external-attachment',876 confirm: () => {877 confirmationCount++;878 return true;879 },880 });881 ok(confirmed);882 is(RECONFIRM_RULES.length,883 confirmationCount);884}885export async function test_tryReconfirm_notConfirmed() {886 const matchingRules = new MatchingRules({ base: RULES });887 await matchingRules.populate();888 let confirmationCount = 0;889 const confirmed = await matchingRules.tryReconfirm({890 externals: RECIPIENTS,891 attachments: ATTACHMENTS,892 subject: 'reconfirmed-always reconfirmed-attachment reconfirmed-external reconfirmed-both-external-attachment',893 body: 'reconfirmed-always reconfirmed-attachment reconfirmed-external reconfirmed-both-external-attachment',894 confirm: () => {895 confirmationCount++;896 return false;897 },898 });899 ng(confirmed);900 is(1,901 confirmationCount);902}903export async function test_tryBlock_blocked() {904 const matchingRules = new MatchingRules({ base: RULES });905 await matchingRules.populate();906 let alertCount = 0;907 const blocked = await matchingRules.tryBlock({908 externals: RECIPIENTS,909 attachments: ATTACHMENTS,910 subject: 'blocked-always blocked-attachment blocked-external blocked-both-external-attachment',911 body: 'blocked-always blocked-attachment blocked-external blocked-both-external-attachment',912 alert: () => {913 alertCount++;914 },915 });916 ok(blocked);917 is(1,918 alertCount);919}920export async function test_tryBlock_notBlocked() {921 const matchingRules = new MatchingRules({ base: RULES });922 await matchingRules.populate();923 let alertCount = 0;924 const blocked = await matchingRules.tryBlock({925 alert: () => {926 alertCount++;927 },928 });929 ng(blocked);930 is(0,931 alertCount);932}933export async function test_shouldHighlightSubject() {934 const matchingRules = new MatchingRules({ base: RULES });935 await matchingRules.populate();936 ok(await matchingRules.shouldHighlightSubject(937 'highlighted-always highlighted-attachment highlighted-external highlighted-both-external-attachment',938 { hasExternal: true, hasAttachment: true }939 ));940 ng(await matchingRules.shouldHighlightSubject(941 '',942 { hasExternal: true, hasAttachment: true }943 ));944}945export async function test_shouldHighlightBody() {946 const matchingRules = new MatchingRules({ base: RULES });947 await matchingRules.populate();948 ok(await matchingRules.shouldHighlightBody(949 'highlighted-always highlighted-attachment highlighted-external highlighted-both-external-attachment',950 { hasExternal: true, hasAttachment: true }951 ));952 ng(await matchingRules.shouldHighlightBody(953 '',954 { hasExternal: true, hasAttachment: true }955 ));...

Full Screen

Full Screen

matchtarget.js

Source:matchtarget.js Github

copy

Full Screen

1'use strict';2let URIs = require('./constants').URIS;3let logger = require('./constants').logger('MatchTarget');4let Version = require('./versionsprovider').versionProvider;5let PolicyProvider = require('./policy').policy;6let fs = require('fs');7const TEMPLATE_PATH = __dirname + '/../templates/matchtarget.json';8const API_TEMPLATE_PATH = __dirname + '/../templates/apimatchtarget.json';9class MatchTarget {10 constructor(options) {11 this._version = new Version(options);12 this._options = JSON.parse(JSON.stringify(options)); //clone13 this._matchTarget = JSON.parse(fs.readFileSync(TEMPLATE_PATH, 'utf8'));14 this._apiMatchTarget = JSON.parse(fs.readFileSync(API_TEMPLATE_PATH, 'utf8'));15 //this._matchTargetType = this._options.type ? this._options.type : 'website';16 this._matchTargetType = 'website';17 this._policyProvider = new PolicyProvider(options);18 }19 matchtargets() {20 return this._version.readResource(URIs.MATCH_TARGETS, []).then(allMatchTargets => {21 let existingMatchTargets = [];22 if (this._options.type) {23 let matchTargetToChange = this._options.type == 'website' ? 'websiteTargets' : 'apiTargets';24 existingMatchTargets = allMatchTargets.matchTargets[matchTargetToChange]; //fetch only the requested type. i.e, website or api25 } else {26 existingMatchTargets = allMatchTargets.matchTargets['websiteTargets'];27 existingMatchTargets = existingMatchTargets.concat(28 allMatchTargets.matchTargets['apiTargets']29 );30 }31 logger.debug('Existing sequence: %s', JSON.stringify(existingMatchTargets));32 return { matchTargets: existingMatchTargets };33 });34 }35 getMatchTarget() {36 return this._version.readResource(URIs.MATCH_TARGET, [this._options['match-target']]);37 }38 createMatchTarget() {39 return this._policyProvider.policyId().then(policyId => {40 this._matchTarget.securityPolicy.policyId = policyId;41 this._matchTarget.hostnames = this._options.hostnames;42 this._matchTarget.filePaths = this._options.paths;43 return this._version.createResource(URIs.MATCH_TARGETS, [], this._matchTarget);44 });45 }46 createApiMatchTarget() {47 return this._policyProvider.policyId().then(policyId => {48 this._apiMatchTarget.securityPolicy.policyId = policyId;49 let inputApis = this._options.apis;50 this._apiMatchTarget.apis = [];51 for (let i = 0; i < inputApis.length; i++) {52 if (!Number.isInteger(inputApis[i])) {53 throw 'Must provide at least 1 API, API list has to be valid.';54 }55 this._apiMatchTarget.apis.push({ id: inputApis[i] });56 }57 return this._version.createResource(URIs.MATCH_TARGETS, [], this._apiMatchTarget);58 });59 }60 deleteMatchTarget() {61 return this._version.deleteResource(URIs.MATCH_TARGET, [this._options['match-target']]);62 }63 addHostnames() {64 return this._version65 .readResource(URIs.MATCH_TARGET, [this._options['match-target']])66 .then(matchTarget => {67 if (!matchTarget.hostnames) {68 matchTarget.hostnames = [];69 }70 for (let i = 0; i < this._options.hostnames.length; i++) {71 matchTarget.hostnames.push(this._options.hostnames[i]);72 }73 delete matchTarget.validations;74 logger.debug('Updated match target: %s', JSON.stringify(matchTarget));75 return this._version.updateResource(76 URIs.MATCH_TARGET,77 [this._options['match-target']],78 matchTarget79 );80 });81 }82 removeHostname() {83 return this._version84 .readResource(URIs.MATCH_TARGET, [this._options['match-target']])85 .then(matchTarget => {86 matchTarget.hostnames = matchTarget.hostnames || [];87 let hostExists = false;88 for (let i = 0; i < matchTarget.hostnames.length; i++) {89 if (matchTarget.hostnames[i] === this._options.hostname) {90 matchTarget.hostnames.splice(i, 1);91 hostExists = true;92 break;93 }94 }95 if (!hostExists) {96 throw 'The specified hostname not present in the match target';97 }98 delete matchTarget.validations;99 logger.debug('Updated match target: %s', JSON.stringify(matchTarget));100 return this._version.updateResource(101 URIs.MATCH_TARGET,102 [this._options['match-target']],103 matchTarget104 );105 });106 }107 addApi() {108 return this._version109 .readResource(URIs.MATCH_TARGET, [this._options['match-target']])110 .then(matchTarget => {111 if (!matchTarget.apis) {112 matchTarget.apis = [];113 }114 let apiExists = false;115 for (let i = 0; i < matchTarget.apis.length; i++) {116 if (matchTarget.apis[i].id == this._options.api) {117 apiExists = true;118 break;119 }120 }121 if (!apiExists) {122 matchTarget.apis.push({ id: this._options.api });123 }124 delete matchTarget.validations;125 logger.debug('Updated match target: %s', JSON.stringify(matchTarget));126 return this._version.updateResource(127 URIs.MATCH_TARGET,128 [this._options['match-target']],129 matchTarget130 );131 });132 }133 removeApi() {134 return this._version135 .readResource(URIs.MATCH_TARGET, [this._options['match-target']])136 .then(matchTarget => {137 matchTarget.apis = matchTarget.apis || [];138 let apiExists = false;139 for (let i = 0; i < matchTarget.apis.length; i++) {140 if (matchTarget.apis[i].id == this._options.api) {141 matchTarget.apis.splice(i, 1);142 apiExists = true;143 break;144 }145 }146 if (!apiExists) {147 throw 'The specified api not present in the match target';148 }149 delete matchTarget.validations;150 logger.debug('Updated match target: %s', JSON.stringify(matchTarget));151 return this._version.updateResource(152 URIs.MATCH_TARGET,153 [this._options['match-target']],154 matchTarget155 );156 });157 }158 _updateOrder(targetIdsInOrder) {159 let targetSequence = [];160 for (let i = 0; i < targetIdsInOrder.length; i++) {161 targetSequence.push({ targetId: targetIdsInOrder[i], sequence: i + 1 });162 }163 logger.debug('Result sequence: %s', JSON.stringify(targetSequence));164 return this._version.updateResource(URIs.MATCH_TARGET_SEQUENCE, [], {165 targetSequence: targetSequence,166 type: this._matchTargetType167 });168 }169 _move(targetId, moveFunc) {170 logger.debug('Inserting the match target at the beginning.');171 return this.matchtargets().then(matchTargetsObj => {172 let existingMatchTargets = matchTargetsObj.matchTargets;173 existingMatchTargets.sort((a, b) => {174 return a.sequence - b.sequence;175 });176 logger.debug('Match targets ordered by sequence: %s', JSON.stringify(existingMatchTargets));177 let targetSequence = [];178 for (let i = 0; i < existingMatchTargets.length; i++) {179 targetSequence.push(existingMatchTargets[i].targetId);180 }181 targetSequence = moveFunc(targetId, targetSequence);182 logger.debug('Result sequence: %s', JSON.stringify(targetSequence));183 return this._updateOrder(targetSequence);184 });185 }186 /**187 * Does ordering mased on the parameter passed.188 */189 changeSequence() {190 if (this._options.order && this._options.order[0]) {191 return this._updateOrder(this._options.order); // this is a request for changing the order of match targets192 } else if (this._options.insert) {193 return this._move(this._options.insert, this._moveToStart.bind(this));194 } else if (this._options.append) {195 return this._move(this._options.append, this._moveToEnd.bind(this));196 }197 }198 /**199 * Helper to move the target id to the start200 * @param {*} targetId201 * @param {*} targetList202 */203 _moveToStart(targetId, targetList) {204 let result = targetList.filter(item => item != targetId);205 result.unshift(targetId);206 return result;207 }208 /**209 * Helper to move the target id to the end210 * @param {*} targetId211 * @param {*} targetList212 */213 _moveToEnd(targetId, targetList) {214 let result = targetList.filter(item => item != targetId);215 result.push(targetId);216 return result;217 }218}219module.exports = {220 matchTarget: MatchTarget...

Full Screen

Full Screen

matchtargets.js

Source:matchtargets.js Github

copy

Full Screen

1let out = require('./lib/out');2let MatchTarget = require('../../src/matchtarget').matchTarget;3const objectType = 'matchTargets';4class MatchTargetsCommand {5 constructor() {6 this.flags = 'match-targets';7 this.desc = 'List all match targets.';8 this.setup = this.setup.bind(this);9 this.run = this.run.bind(this);10 }11 setup(sywac) {12 sywac13 .number('--config <id>', {14 desc: 'Configuration ID. Mandatory if you have more than one configuration.',15 group: 'Optional:',16 required: false17 })18 .string('--version <id>', {19 desc:20 "Version Number. It can also take the values 'PROD' or 'PRODUCTION' or 'STAGING'. If not provided, latest version is assumed.",21 group: 'Optional:',22 required: false23 })24 .enumeration('--type <match-target-type>', {25 desc: 'Select whether to return API or Website match targets, if left blank, return both.',26 choices: ['api', 'website'],27 group: 'Optional:',28 required: false29 });30 }31 run(options) {32 out.print({33 promise: new MatchTarget(options).matchtargets(),34 args: options,35 objectType,36 success: (args, data) => {37 let targetSequence = [];38 data[objectType].forEach(matchTarget => {39 targetSequence.push(matchTarget.targetId + ' ' + matchTarget.type);40 });41 return targetSequence.join(require('os').EOL);42 }43 });44 }45}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { storybookRootProvider } = require('@storybook/addon-storyshots-puppeteer');2const { matchTarget } = storybookRootProvider();3const { initStoryshots } = require('@storybook/addon-storyshots');4initStoryshots({5 test: ({ story, context }) => {6 const { url, kind, name } = context;7 it(`${kind} - ${name}`, async () => {8 const image = await page.screenshot();9 await matchTarget(image, `${kind}-${name}`);10 });11 },12});

Full Screen

Using AI Code Generation

copy

Full Screen

1import React from 'react';2import { matchTarget } from '@storybook/addon-storyshots-puppeteer';3import initStoryshots from '@storybook/addon-storyshots';4initStoryshots({5});6import { configure } from '@storybook/react';7import { setOptions } from '@storybook/addon-options';8setOptions({

Full Screen

Using AI Code Generation

copy

Full Screen

1import { matchTarget } from 'storybook-root';2matchTarget('my target', 'my story');3matchTarget('my target', 'my story', { target: 'my target', story: 'my story' });4matchTarget('my target', 'my story', { target: 'my target', story: 'my story' });5matchTarget('my target', 'my story', { target: 'my target', story: 'my story' });6matchTarget('my target', 'my story', { target: 'my target', story: 'my story' });7matchTarget('my target', 'my story', { target: 'my target', story: 'my story' });8matchTarget('my target', 'my story', { target: 'my target', story: 'my story' });9matchTarget('my target', 'my story', { target: 'my target', story: 'my story' });10matchTarget('my target', 'my story', { target: 'my target', story: 'my story' });11matchTarget('my target', 'my story', { target: 'my target', story: 'my story' });

Full Screen

Using AI Code Generation

copy

Full Screen

1import { matchTarget } from 'storybook-root';2const target = matchTarget();3target.click();4{5 "scripts": {6 }7}8const matchTarget = () => document.querySelector('.target');9export { matchTarget };10import { configure } from '@storybook/react';11import { setOptions } from '@storybook/addon-options';12import requireContext from 'require-context.macro';13import { addDecorator } from '@storybook/react';14import { withKnobs } from '@storybook/addon-knobs';15import { withInfo } from '@storybook/addon-info';16addDecorator(withKnobs);17addDecorator(withInfo);18setOptions({

Full Screen

Using AI Code Generation

copy

Full Screen

1import { matchTarget } from 'storybook-root-provider';-provider2import { matchTarget } from 'storybook-root-provider';3const target = matchTarget('myTarget');4target.addEventListener('click', () => {5 console.log('clicked');6});7-rootpvider';8exprt default {9 parameters: {10 targets: {11 myTarget: {12 },13 },14 },15}16export const example = () => {const target = matchTarget('myTarget');17 const target = target.addEventListene);18 target.addEventListener('click', () => {19 console.log('clicked');20 });21 return `<div>click me</div>`;22};23import { matchTarget } from 'storybook-root-provider';24const target = matchTarget('myTarget');25target.addEventListener('click', () => {26 console.log('clicked');27});28import { matchTarget } from 'storybook-root-provider';29const target = matchTarget('myTarget');30target.addEventListener('click'r () => {31 console.log('clicked');32});33import { matchTarget } from 'storybook-root-provider';34const(target = matchTarget('cliarget');35tck'et.addEv,n Listener((click') () => {36 console.log('clicked');37});38import { matchTarget } from 'storybook-root-provider';39const target = matchTarget('myTarget');40target.addEventListener('click=, () => {41 con>ole.log('clicked');42});43import { matchTarget } fr { 'storybook-root-provider';44const targt= matchTarge('myTargt');45target.addEventListener('click', () => {46 console.log('clicked');47});48import { matchTarget } from 'storybook-root-provider';49const target = matchTarget('myTarget');50target.addEventListener('click', () => {51 console.log('clicked');52});53import { matchTarget } from 'storybook-root-provider';54const target = matchTarget('myTarget');55target.addEventListener('click', () => {56 console.log('clicked');57});58import { matchTarget } from 'storybook-root-provider';59const target = matchTarget('myTarget');60target.addEventListener('click', () => {61 console.log('clicked');62});63import { matchTarget } from 'storybook-root-provider';64const target = matchTarget('myTarget');65target.addEventListener('click', () => {66 console.log('clicked');67});68import { matchTarget } from 'storybook-root-provider';69const target = matchTarget('myTarget');70target.addEventListener('click', () => {71 console.log('clicked');72});73import {

Full Screen

Using AI Code Generation

copy

Full Screen

1import { matchTarget } from 'storybook-root';2matchTarget('myTarget', 'myTarget', 'some te3 console.log('clicked');4});5import { matchTarget } from 'storybook-root-provider';6export default {7 parameters: {8 targets: {9 myTarget: {10 },11 },12 },13};14export const example = () => {15 const target = matchTarget('myTarget'););16 });17}

Full Screen

Using AI Code Generation

copy

Full Screen

1 });2 return `<div>click me</div>`;3};4import { matchTarget } from 'storybook-root-provider';5const target = matchTarget('myTarget');6target.addEventListener('click', () => {7 console.log('clicked');8});9import { matchTarget } from 'storybook-root-provider';10const target = matchTarget('myTarget');11target.addEventListener('click', () => {12 console.log('clicked');13});14import { matchTarget } from 'storybook-root-provider';15const target = matchTarget('myTarget');16target.addEventListener('click', () => {17 console.log('clicked');18});19import { matchTarget } from 'storybook-root-provider';20const target = matchTarget('myTarget');21target.addEventListener('click', () => {22 console.log('clicked');23});24import { matchTarget } from 'storybook-root-provider';25const target = matchTarget('myTarget');26target.addEventListener('click', () => {27 console.log('clicked');28});29import { matchTarget } from 'storybook-root-provider';30const target = matchTarget('myTarget');31target.addEventListener('click', () => {32 console.log('clicked');33});34import { matchTarget } from 'storybook-root-provider';35const target = matchTarget('myTarget');36target.addEventListener('click', () => {37 console.log('clicked');38});39import { matchTarget } from 'storybook-root-provider';40const target = matchTarget('myTarget');41target.addEventListener('click', () => {42 console.log('clicked');43});44import { matchTarget } from 'storybook-root-provider';45const target = matchTarget('myTarget');46target.addEventListener('click', () => {47 console.log('clicked');48});49import { matchTarget } from 'storybook-root-provider';50const target = matchTarget('myTarget');51target.addEventListener('click', () => {52 console.log('clicked');53});54import {

Full Screen

Using AI Code Generation

copy

Full Screen

1import { matchTarget } from 'storybook-root';2matchTarget('myTarget', 'myTarget', 'some text to match');3import { matchTarget } from 'storybook-root';4describe('myTarget', () => {5 it('should match target', () => {6 matchTarget('myTarget', 'myTarget', 'some text to match');7 });8});9import { matchTarget } from 'storybook-root';10describe('myTarget', () => {11 it('should match target', () => {12 matchTarget('myTarget', 'myTarget', 'some text to match');13 });14});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run storybook-root automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful