Skip to content

Commit 0cc12f4

Browse files
committed
Python: Add test for instances
1 parent f1a9637 commit 0cc12f4

9 files changed

Lines changed: 43 additions & 6 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Defines a InlineExpectationsTest for class instances, that is,
3+
* for any API::Node that is an instance of a class (e.g. `Flask`).
4+
*/
5+
6+
import python
7+
import semmle.python.ApiGraphs
8+
import utils.test.InlineExpectationsTest
9+
private import semmle.python.dataflow.new.internal.PrintNode
10+
11+
signature API::Node getInstanceSig();
12+
13+
module MakeInlineInstanceTest<getInstanceSig/0 getInstance> {
14+
private module InlineInstanceTest implements TestSig {
15+
string getARelevantTag() { result = "instance" }
16+
17+
predicate hasActualResult(Location location, string element, string tag, string value) {
18+
exists(location.getFile().getRelativePath()) and
19+
exists(API::Node instance | instance = getInstance() |
20+
location = instance.getLocation() and
21+
element = prettyNode(instance.asSource()) and
22+
value = "" and
23+
tag = "instance"
24+
)
25+
}
26+
}
27+
28+
import MakeTest<InlineInstanceTest>
29+
}

python/ql/test/library-tests/frameworks/flask/InlineInstanceTest.expected

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import python
2+
import semmle.python.frameworks.Flask
3+
import semmle.python.ApiGraphs
4+
import experimental.meta.InlineInstanceTest
5+
6+
API::Node getInstance() { result = Flask::FlaskApp::instance() }
7+
8+
import MakeInlineInstanceTest<getInstance/0>

python/ql/test/library-tests/frameworks/flask/old_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import flask
22

33
from flask import Flask, request, make_response
4-
app = Flask(__name__)
4+
app = Flask(__name__) # $ instance
55

66
@app.route("/") # $ routeSetup="/"
77
def hello_world(): # $ requestHandler

python/ql/test/library-tests/frameworks/flask/response_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from flask import Flask, make_response, jsonify, Response, request, redirect
44
from werkzeug.datastructures import Headers
55

6-
app = Flask(__name__)
6+
app = Flask(__name__) # $ instance
77

88

99
@app.route("/html1") # $ routeSetup="/html1"

python/ql/test/library-tests/frameworks/flask/routing_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import flask
22

33
from flask import Flask, make_response
4-
app = Flask(__name__)
4+
app = Flask(__name__) # $ instance
55

66

77
SOME_ROUTE = "/some/route"

python/ql/test/library-tests/frameworks/flask/save_uploaded_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from flask import Flask, request
2-
app = Flask(__name__)
2+
app = Flask(__name__) # $ instance
33

44
@app.route("/save-uploaded-file") # $ routeSetup="/save-uploaded-file"
55
def test_taint(): # $ requestHandler

python/ql/test/library-tests/frameworks/flask/taint_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from flask import Flask, request, render_template_string, stream_template_string
2-
app = Flask(__name__)
2+
app = Flask(__name__) # $ instance
33

44
@app.route("/test_taint/<name>/<int:number>") # $ routeSetup="/test_taint/<name>/<int:number>"
55
def test_taint(name = "World!", number="0", foo="foo"): # $ requestHandler routedParameter=name routedParameter=number

python/ql/test/library-tests/frameworks/flask/template_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from flask import Flask, Response, stream_with_context, render_template_string, stream_template_string
2-
app = Flask(__name__)
2+
app = Flask(__name__) # $ instance
33

44
@app.route("/a") # $ routeSetup="/a"
55
def a(): # $ requestHandler

0 commit comments

Comments
 (0)