Method: projects.test  |  Firebase

Method: projects.test

Test Source for syntactic and semantic correctness. Issues present, if any, will be returned to the caller with a description, severity, and source location.

The test method may be executed with Source. Passing Source is useful for unit testing new rules.

Note that tests run using the REST API use production databases, storage buckets, and related resources. Such testing can incur usage charges. We strongly recommend that you use the Firebase Local Emulator Suite to perform Rules testing, since you can run tests on offline, non-production resources without usage charges.

The following is an example of Source that permits users to upload images to a bucket bearing their user id and matching the correct metadata:

Example

// Users are allowed to subscribe and unsubscribe to the blog.
service firebase.storage {
  match /users/{userId}/images/{imageName} {
      allow write: if userId == request.auth.uid
          && (imageName.matches('*.png$')
          || imageName.matches('*.jpg$'))
          && resource.mimeType.matches('^image/')
  }
}

HTTP request

POST https://firebaserules.googleapis.com/v1/{name=projects/**}:test

The URL uses gRPC Transcoding syntax.

Path parameters

Parameters
name

Request body

The request body contains data with the following structure:

JSON representation
<br>{<br> "source": {<br> object (Source)<br> },<br> "testSuite": {<br> object (TestSuite)<br> }<br>}<br>
Fields
source
testSuite

Response body

If successful, the response body contains data with the following structure:

The response for FirebaseRulesService.TestRuleset.

JSON representation
<br>{<br> "issues": [<br> {<br> object (Issue)<br> }<br> ],<br> "testResults": [<br> {<br> object (TestResult)<br> }<br> ]<br>}<br>
Fields
issues[]
testResults[]

Authorization Scopes

Requires one of the following OAuth scopes:

For more information, see the Authentication Overview.

TestSuite

TestSuite is a collection of TestCase instances that validate the logical correctness of rules. The TestSuite may be referenced in-line within a projects.test invocation or as part of a Release object as a pre-release check.

JSON representation
<br>{<br> "testCases": [<br> {<br> object (TestCase)<br> }<br> ]<br>}<br>
Fields
testCases[]

TestCase

TestCase messages provide the request context and an expectation as to whether the given context will be allowed or denied. Test cases may specify the request, resource, and functionMocks to mock a function call to a service-provided function.

The request object represents context present at request-time.

The resource is the value of the target resource (e.g., metadata of a GCS object or Firestore document) as it appears in persistent storage before the request is executed.

JSON representation
<br>{<br> "expectation": enum (Expectation),<br> "request": value,<br> "resource": value,<br> "functionMocks": [<br> {<br> object (FunctionMock)<br> }<br> ],<br> "pathEncoding": enum (PathEncoding),<br> "expressionReportLevel": enum (ExpressionReportLevel)<br>}<br>
Fields
expectation
request
resource
functionMocks[]
pathEncoding
expressionReportLevel

Expectation

The set of supported test case expectations.

Enums
EXPECTATION_UNSPECIFIED
ALLOW
DENY

FunctionMock

Mock Rules function definition.

Mocks must refer to a function declared by the target service. The type of the function args and result will be inferred at test time. If either the arg or result values are not compatible with function type declaration, the request will be considered invalid.

More than one FunctionMock may be provided for a given function name so long as the Arg matchers are distinct. There may be only one function for a given overload where all Arg values are Arg.any_value.

See also Functions in the Security Rules language.

JSON representation
<br>{<br> "function": string,<br> "args": [<br> {<br> object (Arg)<br> }<br> ],<br> "result": {<br> object (Result)<br> }<br>}<br>
Fields
function
args[]
result

Arg

Arg matchers for the mock function.

JSON representation
<br>{<br> // Union field type can be only one of the following:<br> "exactValue": value,<br> "anyValue": {<br> object<br> }<br> // End of list of possible types for union field type.<br>}<br>
Fields
Union field type. Supported argument values. type can be only one of the following:
exactValue
anyValue

Result

Possible result values from the function mock invocation.

JSON representation
<br>{<br> // Union field type can be only one of the following:<br> "value": value,<br> "undefined": {<br> object<br> }<br> // End of list of possible types for union field type.<br>}<br>
Fields
Union field type. Supported result values. type can be only one of the following:
value
undefined

PathEncoding

The type of path encoding used.

Enums
ENCODING_UNSPECIFIED
URL_ENCODED
PLAIN

ExpressionReportLevel

The amount of data to include in the expression report response.

Enums
LEVEL_UNSPECIFIED
NONE
FULL
VISITED

Issue

Issues include warnings, errors, and deprecation notices.

JSON representation
<br>{<br> "sourcePosition": {<br> object (SourcePosition)<br> },<br> "description": string,<br> "severity": enum (Severity)<br>}<br>
Fields
sourcePosition
description
severity

SourcePosition

Position in the Source content including its line, column number, and an index of the File in the Source message. Used for debug purposes.

JSON representation
<br>{<br> "fileName": string,<br> "line": integer,<br> "column": integer,<br> "currentOffset": integer,<br> "endOffset": integer<br>}<br>
Fields
fileName
line
column
currentOffset
endOffset

Severity

The set of issue severities.

Enums
SEVERITY_UNSPECIFIED
DEPRECATION
WARNING
ERROR

TestResult

Test result message containing the state of the test as well as a description and source position for test failures.

JSON representation
<br>{<br> "state": enum (State),<br> "debugMessages": [<br> string<br> ],<br> "errorPosition": {<br> object (SourcePosition)<br> },<br> "functionCalls": [<br> {<br> object (FunctionCall)<br> }<br> ],<br> "visitedExpressions": [<br> {<br> object (VisitedExpression)<br> }<br> ],<br> "expressionReports": [<br> {<br> object (ExpressionReport)<br> }<br> ]<br>}<br>
Fields
state
debugMessages[]
errorPosition
functionCalls[]
visitedExpressions[]
expressionReports[]

State

Valid states for the test result.

Enums
STATE_UNSPECIFIED
SUCCESS
FAILURE

FunctionCall

Represents a service-defined function call that was invoked during test execution.

JSON representation
<br>{<br> "function": string,<br> "args": [<br> value<br> ]<br>}<br>
Fields
function
args[]

VisitedExpression

Store the position and access outcome for an expression visited in rules.

JSON representation
<br>{<br> "sourcePosition": {<br> object (SourcePosition)<br> },<br> "value": value<br>}<br>
Fields
sourcePosition
value

ExpressionReport

Describes where in a file an expression is found and what it was evaluated to over the course of its use.

JSON representation
<br>{<br> "sourcePosition": {<br> object (SourcePosition)<br> },<br> "values": [<br> {<br> object (ValueCount)<br> }<br> ],<br> "children": [<br> {<br> object (ExpressionReport)<br> }<br> ]<br>}<br>
Fields
sourcePosition
values[]
children[]

ValueCount

Tuple for how many times an Expression was evaluated to a particular ExpressionValue.

JSON representation
<br>{<br> "value": value,<br> "count": integer<br>}<br>
Fields
value
count