1+ import assert from 'assert' ;
2+ import sinon from 'sinon' ;
3+ import auth from '../../../../Auth.js' ;
4+ import { cli } from '../../../../cli/cli.js' ;
5+ import { CommandInfo } from '../../../../cli/CommandInfo.js' ;
6+ import { Logger } from '../../../../cli/Logger.js' ;
7+ import { CommandError } from '../../../../Command.js' ;
8+ import request from '../../../../request.js' ;
9+ import { telemetry } from '../../../../telemetry.js' ;
10+ import { formatting } from '../../../../utils/formatting.js' ;
11+ import { pid } from '../../../../utils/pid.js' ;
12+ import { session } from '../../../../utils/session.js' ;
13+ import { sinonUtil } from '../../../../utils/sinonUtil.js' ;
14+ import { z } from 'zod' ;
15+ import commands from '../../commands.js' ;
16+ import command from './site-alert-remove.js' ;
17+
18+ describe ( commands . SITE_ALERT_REMOVE , ( ) => {
19+ let log : any [ ] ;
20+ let logger : Logger ;
21+ let commandInfo : CommandInfo ;
22+ let commandOptionsSchema : z . ZodTypeAny ;
23+ let confirmationPromptStub : sinon . SinonStub ;
24+
25+ const webUrl = 'https://contoso.sharepoint.com/sites/marketing' ;
26+ const alertId = '39d9e102-9e8f-4e74-8f17-84a92f972fcf' ;
27+
28+ before ( ( ) => {
29+ sinon . stub ( auth , 'restoreAuth' ) . resolves ( ) ;
30+ sinon . stub ( telemetry , 'trackEvent' ) . resolves ( ) ;
31+ sinon . stub ( pid , 'getProcessName' ) . returns ( '' ) ;
32+ sinon . stub ( session , 'getId' ) . returns ( '' ) ;
33+ commandInfo = cli . getCommandInfo ( command ) ;
34+ commandOptionsSchema = commandInfo . command . getSchemaToParse ( ) ! ;
35+ auth . connection . active = true ;
36+ } ) ;
37+
38+ beforeEach ( ( ) => {
39+ log = [ ] ;
40+ logger = {
41+ log : async ( msg : string ) => {
42+ log . push ( msg ) ;
43+ } ,
44+ logRaw : async ( msg : string ) => {
45+ log . push ( msg ) ;
46+ } ,
47+ logToStderr : async ( msg : string ) => {
48+ log . push ( msg ) ;
49+ }
50+ } ;
51+ confirmationPromptStub = sinon . stub ( cli , 'promptForConfirmation' ) . resolves ( false ) ;
52+ } ) ;
53+
54+ afterEach ( ( ) => {
55+ sinonUtil . restore ( [
56+ request . delete ,
57+ cli . promptForConfirmation
58+ ] ) ;
59+ } ) ;
60+
61+ after ( ( ) => {
62+ sinon . restore ( ) ;
63+ auth . connection . active = false ;
64+ } ) ;
65+
66+ it ( 'has correct name' , ( ) => {
67+ assert . strictEqual ( command . name , commands . SITE_ALERT_REMOVE ) ;
68+ } ) ;
69+
70+ it ( 'has a description' , ( ) => {
71+ assert . notStrictEqual ( command . description , null ) ;
72+ } ) ;
73+
74+ it ( 'fails validation if webUrl is not a valid URL' , async ( ) => {
75+ const actual = commandOptionsSchema . safeParse ( { webUrl : 'foo' , id : alertId } ) ;
76+ assert . strictEqual ( actual . success , false ) ;
77+ } ) ;
78+
79+ it ( 'fails validation if alertId is not a valid GUID' , async ( ) => {
80+ const actual = commandOptionsSchema . safeParse ( { webUrl : webUrl , id : 'invalid' } ) ;
81+ assert . strictEqual ( actual . success , false ) ;
82+ } ) ;
83+
84+ it ( 'passes validation when valid webUrl and alertId are provided' , async ( ) => {
85+ const actual = commandOptionsSchema . safeParse ( { webUrl : webUrl , id : alertId } ) ;
86+ assert . strictEqual ( actual . success , true ) ;
87+ } ) ;
88+
89+ it ( 'prompts before removing the alert' , async ( ) => {
90+ await command . action ( logger , { options : { webUrl : webUrl , id : alertId } } ) ;
91+ assert ( confirmationPromptStub . calledOnce ) ;
92+ } ) ;
93+
94+ it ( 'aborts removing the alert when prompt is not confirmed' , async ( ) => {
95+ const deleteStub = sinon . stub ( request , 'delete' ) . resolves ( ) ;
96+
97+ await command . action ( logger , { options : { webUrl : webUrl , id : alertId } } ) ;
98+ assert ( deleteStub . notCalled ) ;
99+ } ) ;
100+
101+ it ( 'correctly removes the alert' , async ( ) => {
102+ const deleteStub = sinon . stub ( request , 'delete' ) . callsFake ( async ( opts ) => {
103+ if ( opts . url === `${ webUrl } /_api/web/Alerts/DeleteAlert('${ formatting . encodeQueryParameter ( alertId ) } ')` ) {
104+ return ;
105+ }
106+
107+ throw 'Invalid request: ' + opts . url ;
108+ } ) ;
109+
110+ await command . action ( logger , { options : { webUrl : webUrl , id : alertId , force : true , verbose : true } } ) ;
111+ assert ( deleteStub . calledOnce ) ;
112+ } ) ;
113+
114+ it ( 'handles error correctly' , async ( ) => {
115+ const error = {
116+ error : {
117+ 'odata.error' : {
118+ code : '-2146232832, Microsoft.SharePoint.SPException' ,
119+ message : {
120+ value : 'The alert you are trying to access does not exist or has just been deleted.'
121+ }
122+ }
123+ }
124+ } ;
125+ sinon . stub ( request , 'delete' ) . rejects ( error ) ;
126+
127+ await assert . rejects ( command . action ( logger , { options : { force : true , webUrl : webUrl , id : alertId } } ) ,
128+ new CommandError ( error . error [ 'odata.error' ] . message . value ) ) ;
129+ } ) ;
130+ } ) ;
0 commit comments