3分钟阅读
介绍
在本文中,我们将学习如何自动化REST API的验收测试。该API提供了端点来注册和验证用户(注册/登录)。此外,还提供了一个安全的端点来获取用户数据。该服务将保留并获取mongo数据库中的数据。
下面的流程图表示该服务的工作方式。
> API Flow diagram
先决条件
克隆包含以下内容的Git存储库
git clone git@github:ivancorrales/orion-test-api.git
此外,您应该在计算机上安装Docker和Orion。
Orion-test-api
我们将浏览存储库中的不同文件和文件夹。
> repository content
docker-compose.yml
服务和mongo数据库的规范。
version: ‘3’
services:
app:
image: ivancorrales/demo-api-mongo:0.0.1
environment:
MONGODB_URI: mongodb://root:secret@mongodb:27017/admin
ports:
- 3000:3000
mongodb:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: secret
ports:
- 27017:27017
volumes:
- mongodb_data_container:/data/db
volumes:
mongodb_data_container:
让我们执行docker-compose来启动服务和mongo容器。
.env
它包含要传递给Orion的环境变量。
.includes
文件夹include / *中的api.hcl和database.hcl文件包含可重用的函数来处理API和数据库。
main.hcl
该文件将用作执行所有方案的入口点。
happyPath.hcl
涵盖幸福道路的场景。
signIn-failures.hcl / signUp-failures.hcl / showProfile-failures.
一组场景,涵盖了不愉快的道路。
我们开始做吧!
首先,我们使用docker-compose up命令启动docker-compose。服务API在localhost:3000上运行,而mongo在localhost:27017上运行。请记住,连接到mongo(root / secret)需要身份验证。
验收测试可以使用以下命令执行:
orion run --input main.hcl --vars env/local.hcl
但…。让我们深入研究Orion文件!
包括/ api.hcl
它包含使用API端点的函数。这些功能是从方案中调用的。
实际上,阅读功能非常简单。您将意识到showProfile函数需要传递标头x-access-token,因为这是一个安全的端点。
了解有关var,输入参数,函数和操作的更多信息http
vars {
mediaJson=“application/json”
}
func signUp {
input {
arg baseApi {
description=“URL base of the API”
}
arg user {
description=“entity with attributes fullName, username & password”
}
}
body {
http post {
request {
baseUrl=baseApi
path="/account"
headers {
Content-Type=mediaJson
}
payload json {
data=user
}
}
response {
httpResponse={
statusCode=_.http.statusCode
body=json(_.http.body)
}
}
}
}
return {
value=httpResponse
}
}
func signIn {
input {
arg baseApi {
description=“URL base of the API”
}
arg credentials {
description=“entity with attributes username & password”
}
}
body {
http post {
request {
baseUrl=baseApi
path="/session"
headers {
Content-Type=mediaJson
}
payload json {
data=credentials
}
}
response {
httpResponse={
statusCode=_.http.statusCode
body=json(_.http.body)
}
}
}
}
return {
value=httpResponse
}
}
func showProfile {
input {
arg baseApi {
description=“URL base of the API”
}
arg token {
description=“a valid JWT”
}
}
body {
http get {
request {
baseUrl=baseApi
path="/me"
headers {
x-access-token=token
}
}
response {
httpResponse={
statusCode=_.http.statusCode
body=json(_.http.body)
}
}
}
}
return {
value=httpResponse
}
}包括/database.hcl
它包含一个函数,该函数将在执行方案之前从方案中使用以清理数据库。我们为数据库和集合输入参数定义默认值。
func dropCollection {
input {
arg mongoUri {
description=“mongo uri”
}
arg database {
description=“Name of the database”
default=“admin”
}
arg collection {
description=“Name of the collection”
default=“users”
}
arg credentials {
description=“A object with two attributes: username & password”
}
}
body {
mongo drop {
connection {
uri=mongoUri
auth scram-sha-1{
username=credentials.username
password=credentials.password
}
}
query {
database=database
collection=collection
}
}
}
}
了解有关var,函数和action mongo的更多信息
happyPath.hcl
满意路径必须验证用户已注册,通过身份验证减肥方法,然后他/他才能获取她/他的个人资料详细信息。
您将意识到,当我们在include / api.hcl中调用函数时,不会传递参数baseApi。这是由于baseApi将成为main.hcl中的全局变量。
scenario “happy path” {
given “my user details” {
set user {
value={
fullName=“John Smith”
username=“john.smith@mail”
password=“secret”
}
}
}
when “I sign up” {
call signUp{
# block with can be omitted since variable user is already in the scope
with { user=user }
as=“response”
}
}
then “I am registered successfully” {
assert {
assertion=response.statusCode==200
}
}
when “do login” {
call signIn {
with {
credentials={
username=user.username
password=user.password
}
}
as=“response”
}
}
then “I am authenticated successfully” {
assert {
assertion=response.statusCode==200
}
}
when “I fetch my profile details” {
call showProfile{
with { token=response.body.accessToken }
as=“response”
}
}
then “the returned data are ok” {
assert {
assertion=(
response.statusCode==200 &&
response.body.fullName==user.fullName
)
}
}
}
了解有关场景以及操作调用和断言的更多信息。
main.hcl
该文件用作运行所有方案的入口点。负责添加其他hcl文件。除此之外,我们使用此文件来定义全局挂钩并定义输入参数。
共同學習,寫下你的評論
評論加載中...
作者其他優質文章