|
| 1 | +require "../../../spec_helper" |
| 2 | +require "../../../../src/models/code_locator" |
| 3 | +require "../../../../src/analyzer/analyzers/specification/azure_functions" |
| 4 | + |
| 5 | +private def analyze_azure(content : String, function_dir = "MyFunc") |
| 6 | + dir = File.tempname("azure_function") |
| 7 | + Dir.mkdir_p(File.join(dir, function_dir)) |
| 8 | + path = File.join(dir, function_dir, "function.json") |
| 9 | + File.write(path, content) |
| 10 | + locator = CodeLocator.instance |
| 11 | + locator.clear "azure-functions-spec" |
| 12 | + locator.push "azure-functions-spec", path |
| 13 | + |
| 14 | + options = create_test_options |
| 15 | + analyzer = Analyzer::Specification::AzureFunctions.new options |
| 16 | + analyzer.analyze |
| 17 | +ensure |
| 18 | + if dir |
| 19 | + File.delete(path) if path && File.exists?(path) |
| 20 | + Dir.delete(File.join(dir, function_dir)) if Dir.exists?(File.join(dir, function_dir)) |
| 21 | + Dir.delete(dir) if Dir.exists?(dir) |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | +private def tag_descriptions(endpoint : Endpoint, name : String) : Array(String) |
| 26 | + endpoint.tags.select { |t| t.name == name }.map(&.description) |
| 27 | +end |
| 28 | + |
| 29 | +describe "Azure Functions Analyzer" do |
| 30 | + it "extracts httpTrigger methods + route" do |
| 31 | + endpoints = analyze_azure(<<-JSON, "Users") |
| 32 | + { |
| 33 | + "bindings": [ |
| 34 | + { |
| 35 | + "type": "httpTrigger", |
| 36 | + "direction": "in", |
| 37 | + "name": "req", |
| 38 | + "methods": ["get", "post"], |
| 39 | + "route": "users/{id?}", |
| 40 | + "authLevel": "function" |
| 41 | + } |
| 42 | + ] |
| 43 | + } |
| 44 | + JSON |
| 45 | + |
| 46 | + endpoints.map { |e| {e.url, e.method} }.sort!.should eq([ |
| 47 | + {"/users/{id?}", "GET"}, |
| 48 | + {"/users/{id?}", "POST"}, |
| 49 | + ]) |
| 50 | + endpoints.each { |e| tag_descriptions(e, "azure-auth-level").should eq ["function"] } |
| 51 | + end |
| 52 | + |
| 53 | + it "falls back to function folder name when route is absent" do |
| 54 | + endpoints = analyze_azure(<<-JSON, "Healthcheck") |
| 55 | + { |
| 56 | + "bindings": [ |
| 57 | + {"type": "httpTrigger", "methods": ["get"]} |
| 58 | + ] |
| 59 | + } |
| 60 | + JSON |
| 61 | + |
| 62 | + endpoints.size.should eq 1 |
| 63 | + endpoints[0].url.should eq "/Healthcheck" |
| 64 | + endpoints[0].method.should eq "GET" |
| 65 | + end |
| 66 | + |
| 67 | + it "defaults method to ANY when methods are not declared" do |
| 68 | + endpoints = analyze_azure(<<-JSON, "AnyFunc") |
| 69 | + { |
| 70 | + "bindings": [ |
| 71 | + {"type": "httpTrigger", "route": "any"} |
| 72 | + ] |
| 73 | + } |
| 74 | + JSON |
| 75 | + |
| 76 | + endpoints.size.should eq 1 |
| 77 | + endpoints[0].method.should eq "ANY" |
| 78 | + end |
| 79 | +end |
0 commit comments