@@ -672,6 +672,81 @@ describe('CodeWhisperer-basicCommands', function () {
672672 reasonDesc : 'Failed to apply edit to the workspace.' ,
673673 } )
674674 } )
675+
676+ it ( 'should apply the edit at the correct range' , async function ( ) {
677+ const fileName = 'sample.py'
678+ const textDocumentMock = createMockDocument (
679+ `from flask import app
680+
681+
682+ @app.route('/')
683+ def execute_input_noncompliant():
684+ from flask import request
685+ module_version = request.args.get("module_version")
686+ # Noncompliant: executes unsanitized inputs.
687+ exec("import urllib%s as urllib" % module_version)
688+ # {/fact}
689+
690+
691+ # {fact [email protected] defects=0} 692+ from flask import app
693+
694+
695+ @app.route('/')
696+ def execute_input_compliant():
697+ from flask import request
698+ module_version = request.args.get("module_version")
699+ # Compliant: executes sanitized inputs.
700+ exec("import urllib%d as urllib" % int(module_version))
701+ # {/fact}` ,
702+ fileName
703+ )
704+ openTextDocumentMock . resolves ( textDocumentMock )
705+ sandbox . stub ( vscode . workspace , 'openTextDocument' ) . value ( openTextDocumentMock )
706+
707+ sandbox . stub ( vscode . WorkspaceEdit . prototype , 'replace' ) . value ( replaceMock )
708+ applyEditMock . resolves ( true )
709+ sandbox . stub ( vscode . workspace , 'applyEdit' ) . value ( applyEditMock )
710+ sandbox . stub ( diagnosticsProvider , 'removeDiagnostic' ) . value ( removeDiagnosticMock )
711+ sandbox . stub ( SecurityIssueProvider . instance , 'removeIssue' ) . value ( removeIssueMock )
712+ sandbox . stub ( vscode . window , 'showTextDocument' ) . value ( showTextDocumentMock )
713+
714+ targetCommand = testCommand ( applySecurityFix )
715+ codeScanIssue . suggestedFixes = [
716+ {
717+ code : `@@ -6,4 +6,5 @@
718+ from flask import request
719+ module_version = request.args.get("module_version")
720+ # Noncompliant: executes unsanitized inputs.
721+ - exec("import urllib%d as urllib" % int(module_version))
722+ + __import__("urllib" + module_version)
723+ +#import importlib` ,
724+ description : 'dummy' ,
725+ } ,
726+ ]
727+ await targetCommand . execute ( codeScanIssue , fileName , 'webview' )
728+ assert . ok (
729+ replaceMock . calledOnceWith (
730+ textDocumentMock . uri ,
731+ new vscode . Range ( 5 , 0 , 8 , 54 ) ,
732+ ` from flask import request
733+ module_version = request.args.get("module_version")
734+ # Noncompliant: executes unsanitized inputs.
735+ __import__("urllib" + module_version)
736+ #import importlib`
737+ )
738+ )
739+ assert . ok ( applyEditMock . calledOnce )
740+ assert . ok ( removeDiagnosticMock . calledOnceWith ( textDocumentMock . uri , codeScanIssue ) )
741+ assert . ok ( removeIssueMock . calledOnce )
742+
743+ assertTelemetry ( 'codewhisperer_codeScanIssueApplyFix' , {
744+ detectorId : codeScanIssue . detectorId ,
745+ findingId : codeScanIssue . findingId ,
746+ component : 'webview' ,
747+ result : 'Succeeded' ,
748+ } )
749+ } )
675750 } )
676751
677752 // describe('generateFix', function () {
0 commit comments