Skip to content

Latest commit

 

History

History
807 lines (580 loc) · 18.3 KB

REST-API.md

File metadata and controls

807 lines (580 loc) · 18.3 KB

CometaRestAPIBackend

Any actions you can execute on the Cometa Frontend User Interface can be triggered programmatically via REST API. Cometa uses Django for the backend REST API services. It is implemented in python.

Find below a short overview of REST API endpoints to use. Further down, you will find details on the specific endpoints, such as POST parameters and JSON results to be expected.

This documentation is work in progress and may need some help.

Execute a Feature / Test from Django

  • POST /backend/exectest/

Account roles

  • GET /backend/api/account_roles/

Accounts

  • GET /backend/api/accounts/
  • PATCH /backend/api/accounts/<user_id>/
  • DELETE /backend/api/accounts/<user_id>/

Actions

  • GET /backend/api/actions/

Applications

  • GET /backend/api/applications/
  • POST /backend/api/applications/

Browsers

  • GET /backend/api/browsers/

Departments

  • GET /backend/api/departments/
  • GET /backend/user/departments/

Environments

  • GET /backend/api/environments/
  • POST /api/environments/

Feature Results

  • GET /api/feature_results/<feature_id>/
  • POST /api/feature_results/

Features

  • GET /backend/api/features/
  • POST /api/features/

Folders

  • GET /backend/api/folders/
  • POST /backend/api/folders/
  • PATCH /backend/api/folders
  • DELETE /backend/api/folders/<folder_id>/remove/

Step Results

  • GET /backend/api/feature_results/<feature_result_id>/step_results/
  • POST /backend/api/feature_results/<feature_result_id>/step_results/

Steps

  • GET /backend/steps/<feature_id>/
  • POST /backend/steps/<feature_id>/patch/

Screenshots

  • GET /backend/screenshots/<step_result_id>/
  • GET /backend/screenshot/<screenshot_name>/
  • GET /backend/removeScreenshot/<screenshot_name>/

Variables

  • GET /backend/api/variables/<environment_name>/
  • POST /backend/api/variables/
  • DELETE /backend/api/variables/<variable_id>/

Others

  • GET /backend/addoidcaccount/
  • GET /backend/steps/<step_result_id>/pixel_diff/<pixel_diff>/
  • GET /backend/updateTask/
  • GET /backend/getJson/<feature_id>/
  • GET /backend/killTask/<feature_id>/
  • GET /backend/killTaskPID//
  • GET /backend/stepsByName/
  • GET /backend/schedule/<feature_id>/
  • GET /backend/exectest/
  • GET /backend/info/
  • GET /backend/encrypt/
  • GET /backend/parseActions/
  • GET /backend/browsers/browserstack/
  • GET /backend/feature_results/<feature_result_id>/log/
  • GET /backend/feature_results/<feature_result_id>/remove/<feature_result_id>/
  • GET /backend/feature_runs/<run_id>/remove/

For more details see http://your_server:8000/docs/

All create POST methods returns code 201 and the row inserted if created correctly. All request body parameters should be "application/json", unless specified. All request responses are in JSON format, unless specified.

Execute a Feature / Test from Django

POST /backend/exectest/

Headers
  • HTTP_X_SERVER: Confidential ... or none
  • AMVARAORIGIN: CRONTAB ... or empty
  • Content-Type: application/json
Request body

{
  "feature_id": 169,
  "wait": false
}

Example Curl:

curl -vvv --data '{"feature_id":147,"wait":false}' -H "Content-Type: application/json" -H "X_SERVER: none" -H "AMVARAORIGIN: CRONTAB" -X POST http://behave:8001/run_test/

Implemented by: Alex

Changelog:

  • 13.07.2020 RRO documentation and fixing latest changes breaking scheduling

Account roles

GET /backend/api/account_roles/

Retrieve all available roles for accounts, this is used for Admin --> Accounts and will be empty if the user doesn't have permissions.

Request Examples

http://localhost:8000/backend/api/account_roles/

Output Example

[
  {
    "account_role_id":1,
    "user":1,
    "department":1
  },
  {
    "account_role_id":2,
    "user":2,
    "department":1
  }
]

Accounts

GET /backend/api/accounts/

Retrieve all available accounts in database. Will be empty is user doesn't have enough permissions.

Request Examples

http://localhost:8000/backend/api/accounts/

Output Example

[
  {
    "user_id":2,
    "name":"Alex",
    "email":"[email protected]",
    "user_role":"DEVOPS",
    "created_on":"2020-07-17T13:50:56.584849",
    "last_login":"2020-07-17T21:07:47.920158",
    "favourite_browsers":[],
    "user_permissions": {},
    "departments":["Default"]
  }
]

PATCH /backend/api/accounts/<user_id>/

Modify account information.

Request body

user_id: number;
name: string;
email: string;
user_role: string;
departments: number[];
created_on?: string;
last_login?: string;
Request Examples

http://localhost:8000/backend/api/accounts/26/

DELETE /backend/api/accounts/<user_id>/

Delete an account by UserID

Actions

GET /backend/api/actions/

Retrieve available actions.

Applications

GET /backend/api/applications/

Retrieve available applications for the current user.

Request Examples

http://localhost:8000/backend/api/applications/

Output Example

[
  {
    "app_id": 1,
    "app_name": "App 1",
    "slug": "app-1"
  },
  {
    "app_id": 2,
    "app_name": "App 2",
    "slug": "app-2"
  }
]

POST /backend/api/applications/

Create application.

Request Body

app_name: string;

Browsers

GET /backend/api/browsers/

Retrieve local browsers.

Request Examples

http://localhost:8000/api/browsers/

Output Example

[
  {
    "browser_id": 1,
    "browser_json": {
      "os": "Generic",
      "device": null,
      "browser": "chrome",
      "os_version": "Selenium",
      "real_mobile": false,
      "browser_version": "69"
    }
  }
]

Departments

GET /backend/api/departments/

Retrieve all available departments for the current logged-in user. NOTE: If the current logged-in user has Admin/SUPERUSER permissions, all departments in the database will be retrieved.

Request Examples

http://localhost:8000/backend/api/departments/

Output Example

[
  {
    "department_id": 1,
    "department_name": "Department 1",
    "slug": "department-1"
  },
  {
    "department_id": 2,
    "department_name": "Department 2",
    "slug": "department-2"
  }
]

GET /backend/user/departments/

Retrieve the available departments for the current logged user, only retrieves the departments assigned to the user.

Request Examples

http://localhost:8000/backend/user/departments/

Output Example

[
  {
    "department_id": 1,
    "department_name": "Department 1",
    "slug": "department-1"
  },
  {
    "department_id": 2,
    "department_name": "Department 2",
    "slug": "department-2"
  }
]

Environments

GET /backend/api/environments/

Retrieve environments available for the current logged user.

Request Examples

http://localhost:8000/backend/api/environments/

Output Example

[  
  {  
    "environment_id":1,
    "environment_name":"Environment 1"
  },
  {  
    "environment_id":2,
    "environment_name":"Environment 2"
  }
]

POST /api/environments/

Create environment.

Request Body

environment_name: string;

Feature Results

GET /api/feature_results/<feature_id>/

Retrieve feature results for the given feature_id.

Request Example

http://localhost:8000/api/feature_results/26/

Output Example

[  
  {  
    "feature_result_id":1730421,
    "feature_id":1,
    "result_date":"2018-04-04T22:46:36.247605Z",
    "feature_name":"Feature",
    "app_id":8,
    "app_name":"MIF",
    "environment_id":32,
    "environment_name":"ENV-1",
    "department_id":7,
    "department_name":"Dep-8",
    "browser_id":8,
    "browser_name":"Firefox",
    "total":89,
    "fails":94,
    "ok":67,
    "skipped":55,
    "execution_time":"5.57",
    "pixel_diff":2242,
    "success_rate":91,
    "screen_style":"/code/file.png",
    "screen_actual":"/code/file.png",
    "screen_diff":"/code/file.png"
  }
]

POST /api/feature_results/

Create a new feature result (actually only used for behave).

Request Body

| Parameter | Type | Required | | feature_id | IntegerField | Yes |
| result_date | Datetime | Yes | | feature_name | CharField(max_length=100) | Yes | | app_id | IntegerField | Yes | | app_name | .CharField(max_length=100) | YEs | | environment_id | IntegerField | Yes | | environment_name | CharField(max_length=10) | Yes | | department_id | CharField(max_length=10) | Yes | | department_name | CharField(max_length=100) | Yes | | browser_id | CharField(max_length=10) | Yes | | browser_name | CharField(max_length=100) | Yes | | total | IntegerField | Yes | | fails | IntegerField | Yes | | ok | IntegerField | Yes | | skipped | IntegerField | Yes | | execution_time | DecimalField(decimal_places=2, max_digits=10) | Yes | | pixel_diff | IntegerField | Yes | | success | BooleanField | Yes | | screen_style | CharField(max_length=100) | Yes | | screen_actual | CharField(max_length=100) | Yes | | screen_diff | CharField(max_length=100) | Yes |

Features

GET /backend/api/features/

Retrieve feature information's.

Request Examples

http://localhost:8000/backend/api/features/

POST /api/features/

Create feature.

Request Body

feature_name: string;
app_id: number;
app_name: string;
description: string;
environment_id: number;
environment_name: string;
browser_id: number;
browser_name: string;
browser_code: string;
department_id: number;
department_name: string;
steps: number;
schedule: string;
steps_content: GroupContent[];
wait: boolean;
browsers: BrowserstackBrowser[];
cloud: string;
depends_on_other: boolean;
send_mail: boolean;
email_address: string[];
email_subject: string;
email_body: string;
last_edited: number;
video: boolean;

The schedule is saved in the crontab file inside cometa_behave docker.
To view crontab inside container: root@883b40ddbed0:/opt/code/behave_django# cat /etc/cron.d/crontab To view it outside: cat behave/schedules/crontab

The output when an error occurs can be:

status code 400


{
  "Error": "Invalid app_id , not found in Application model"
}

status code 500


{
  "Error" : "Schedule was not saved"
}

Folders

GET /backend/api/folders/

Retrieve available folders and feature of the current user.

Response schema example

{
  "features": Feature[],
  "folders": [
    {
      "features": Feature[],
      "folder_id": 1,
      "folders": [],
      "name": "New",
      "owner": 1,
      "parent_id": null
    }
  ]
}

POST /backend/api/folders/

Create a folder.

Request body

name: string;
parent_id?: number;

NOTES: If no parent_id is provided, it is null or 0, the parent folder will be the Home folder.

Also, to move a folder to inside another folder, provide the following in the same request:


old_folder: number;
new_folder: number;

Both parameters are IDs.

PATCH /backend/api/folders

Rename a folder by FolderID.

Request body

folder_id: number;
folder_name: string;

DELETE /backend/api/folders/<folder_id>/remove/

Delete a folder by FolderID

Step Results

GET /backend/api/feature_results/<feature_result_id>/step_results/

Retrieve step results for a given feature_result_id.

Request Examples

http://localhost:8000/backend/api/feature_results/18599/step_results/

Output Example

[
  {
    "step_result_id":9919776,
    "feature_result_id":18599,
    "step_name":"Step-9919776",
    "execution_time":"-0.95",
    "pixel_diff":2866,
    "ok":50,
    "fails":39,
    "skipped":96,
    "success":74
  }
]

POST /backend/api/feature_results/<feature_result_id>/step_results/

Create a step result (mostly used for behave).

Request Body

| Parameter | Type | Required | | feature_result_id | IntegerField | Yes |
| step_name | CharField(max_length=100) | Yes | | step_id | IntegerField | Yes | | execution_time | DecimalField(decimal_places=2, max_digits=10) | Yes | | pixel_diff | IntegerField | Yes | | success | BooleanField | Yes |

Steps

GET /backend/steps/<feature_id>/

Retrieve the steps defined for a given feature.

Request Examples

http://localhost:8000/backend/api/steps/

Output Example

[
  {
    "id": 1,
    "feature_id": 1,
    "step_name": "Step 1",
    "enabled": true
  },
  {
    "id": 2,
    "feature_id": 2,
    "step_name": "Step 2"
  }
]

POST /backend/steps/<feature_id>/patch/

Save the feature and steps info.

Request Body

feature_id: number;
steps: any;
description: string;
environment_id: number;
environment_name: string;
app_id: number;
app_name: string;
department_id: number;
department_name: string;
depends_on_other: boolean;
cloud: string;
browsers: BrowserstackBrowser[];
send_mail: boolean;
email_address: string[];
email_subject: string;
email_body: string;
send_mail_on_error: boolean;
last_edited: number;
video: boolean;

Screenshots

GET /backend/screenshots/<step_result_id>/

Retrieve available screenshot for a given step result.

Response example:

["AMVARA_63.png", "AMVARA_63_style.png", "AMVARA_63.png_diff.png"]

GET /backend/screenshot/<screenshot_name>/

Retrieve screenshot by name from Django. NOTES: Django will automatically compress the image at 70%, it will also detect if the requesting browser is WebP compatible and serve the image as WebP instead of JPG.

GET /backend/removeScreenshot/<screenshot_name>/

Delete a screenshot by name from the server. NOTES: If the screenshot removal requested is about the style/template version, another removal request should be done to remove the original template file, for example for a given feature and step index (index of step ignoring previous disabled steps):

http://localhost:8000/backend/removeScreenshot/AMVARA_23_4.png/

Variables

GET /backend/api/variables/<environment_name>/

Retrieve variables names and values for a given environment.

POST /backend/api/variables/

Set variables for a given environment.

Request body

{
  "environment_name": "ENV-3",
  "variables": [
    {
      "variable_name": "password",
      "variable_value": "12345"
    }
  ]
}

DELETE /backend/api/variables/<variable_id>/

Remove variable by it's ID.

Others

GET /backend/addoidcaccount/

Retrieves the OIDC Account information.

GET /backend/steps/<step_result_id>/pixel_diff/<pixel_diff>/

Used from Behave to update the Pixel Difference on running features.

GET /backend/updateTask/

Used from Behave to update the task status of running features.

Request body

action: 'start' | 'finish';
browser: BrowserstackBrowser;
feature_id: number;
pid: number;

GET /backend/getJson/<feature_id>/

Retrieves the JSON information of a given feature.

GET /backend/killTask/<feature_id>/

Kills all running feature results by it's Feature ID.

GET /backend/killTaskPID//

Kill a specific running feature result.

GET /backend/stepsByName/

Retrieves the definition for a given feature name.

Request body

feature_name: string;

GET /backend/schedule/<feature_id>/

Set schedule for a given feature.

Request body

schedule: string;

NOTES: The format for the schedule is the same as in Cron. Leave empty to remove the schedule.

GET /backend/exectest/

Executes a given feature.

Request body

feature_id: number;

GET /backend/info/

Retrieves some useful information from the server, like the compilation version.

GET /backend/encrypt/

Let the user encrypt or decrypt a value using the server token.

Request body

action: 'encrypt' | 'decrypt';
text: string;

GET /backend/parseActions/

Using this API endpoint updates the available actions, parsing the actions.py file and saving it on database. It also sends a WebSocket Event to front in order to automatically update the actions without any reload.

GET /backend/browsers/browserstack/

Retrieves Browsertstack usable browsers.

GET /backend/feature_results/<feature_result_id>/log/

Retrieves the log information for a given feature result.

GET /backend/feature_results/<feature_result_id>/remove/<feature_result_id>/

Removes a feature result by it's ID. It will also remove the parent run object if it is finally empty.

GET /backend/feature_runs/<run_id>/remove/

Removes a feature run by it's ID. It will also remove all child feature results.