go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/frontend/ui/cypress/integration/new_rule_page.spec.ts (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 describe('New Rule Page', () => { 16 beforeEach(() => { 17 // Login. 18 cy.visit('/').contains('Log in').click(); 19 cy.contains('LOGIN').click(); 20 }); 21 it('create rule from scratch', () => { 22 cy.visit('/p/chromium/rules/new'); 23 24 cy.get('new-rule-page').get('[data-cy=bug-system-dropdown]').contains('crbug.com'); 25 cy.get('new-rule-page').get('[data-cy=bug-number-textbox]').get('[type=text]').type('{selectall}101'); 26 cy.get('new-rule-page').get('[data-cy=rule-definition-textbox]').get('textarea').type('{selectall}test = "create test 1"'); 27 28 cy.intercept('POST', '/prpc/luci.analysis.v1.Rules/Create', (req) => { 29 const requestBody = req.body; 30 assert.strictEqual(requestBody.rule.ruleDefinition, 'test = "create test 1"'); 31 assert.deepEqual(requestBody.rule.bug, { system: 'monorail', id: 'chromium/101' }); 32 assert.deepEqual(requestBody.rule.sourceCluster, { algorithm: '', id: '' }); 33 34 const response = { 35 project: 'chromium', 36 // This is a real rule that exists in the dev database, the 37 // same used for rule section UI tests. 38 ruleId: 'ea5305bc5069b449ee43ee64d26d667f', 39 }; 40 // Construct pRPC response. 41 const body = ')]}\'\n' + JSON.stringify(response); 42 req.reply(body, { 43 'X-Prpc-Grpc-Code': '0', 44 }); 45 }).as('createRule'); 46 47 cy.get('new-rule-page').get('[data-cy=create-button]').click(); 48 cy.wait('@createRule'); 49 50 // Verify the rule page loaded. 51 cy.get('body').contains('Associated Bug'); 52 }); 53 it('create rule from cluster', () => { 54 // Use an invalid rule to ensure it does not get created in dev by 55 // accident. 56 const rule = 'test = CREATE_TEST_2'; 57 cy.visit(`/p/chromium/rules/new?rule=${encodeURIComponent(rule)}&sourceAlg=reason-v1&sourceId=1234567890abcedf1234567890abcedf`); 58 59 cy.get('new-rule-page').get('[data-cy=bug-system-dropdown]').contains('crbug.com'); 60 cy.get('new-rule-page').get('[data-cy=bug-number-textbox]').get('[type=text]').type('{selectall}101'); 61 62 cy.intercept('POST', '/prpc/luci.analysis.v1.Rules/Create', (req) => { 63 const requestBody = req.body; 64 assert.strictEqual(requestBody.rule.ruleDefinition, 'test = CREATE_TEST_2'); 65 assert.deepEqual(requestBody.rule.bug, { system: 'monorail', id: 'chromium/101' }); 66 assert.deepEqual(requestBody.rule.sourceCluster, { algorithm: 'reason-v1', id: '1234567890abcedf1234567890abcedf' }); 67 68 const response = { 69 project: 'chromium', 70 // This is a real rule that exists in the dev database, the 71 // same used for rule section UI tests. 72 ruleId: 'ea5305bc5069b449ee43ee64d26d667f', 73 }; 74 // Construct pRPC response. 75 const body = ')]}\'\n' + JSON.stringify(response); 76 req.reply(body, { 77 'X-Prpc-Grpc-Code': '0', 78 }); 79 }).as('createRule'); 80 81 cy.get('new-rule-page').get('[data-cy=create-button]').click(); 82 cy.wait('@createRule'); 83 84 // Verify the rule page loaded. 85 cy.get('body').contains('Associated Bug'); 86 }); 87 it('displays validation errors', () => { 88 cy.visit('/p/chromium/rules/new'); 89 cy.get('new-rule-page').get('[data-cy=bug-system-dropdown]').contains('crbug.com'); 90 cy.get('new-rule-page').get('[data-cy=bug-number-textbox]').get('[type=text]').type('{selectall}101'); 91 cy.get('new-rule-page').get('[data-cy=rule-definition-textbox]').get('textarea').type('{selectall}test = INVALID'); 92 93 cy.get('new-rule-page').get('[data-cy=create-button]').click(); 94 95 cy.get('body').contains('Validation error: rule definition is not valid: undeclared identifier "invalid".'); 96 }); 97 });