我正在创建REST后端(node.js,尽管对该问题并不重要),在这里我通过Google登录对用户进行身份验证。
这就是身份验证和创建用户端点的样子(大张旗鼓(为了简单起见,我删除了所有其他部分):
openapi: 3.0.0
info:
version: 0.1.0
title: XXX API
servers:
- url: 'https://xxx.herokuapp.com/'
- url: 'http://localhost:5000/'
paths:
/auth:
post:
summary: Authenticate user
operationId: authUser
tags:
- auth
parameters:
- name: googleToken
in: query
required: true
schema:
$ref: '#/components/schemas/GoogleToken'
responses:
'200':
$ref: '#/components/responses/LoggedUser'
/users:
post:
summary: Create new user
operationId: createUser
tags:
- users
parameters:
- name: googleToken
in: query
required: true
schema:
$ref: '#/components/schemas/GoogleToken'
responses:
'201':
$ref: '#/components/responses/LoggedUser'
components:
schemas:
UserId:
type: string
GoogleToken:
type: string
User:
type: object
properties:
userId:
$ref: '#/components/schemas/UserId'
responses:
LoggedUser:
description: Successfully logged user
headers:
Auth-Token:
schema:
type: string
example: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJP
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/User'
如您所见,我依赖google令牌进行身份验证和用户创建。根据文档的建议,我正在使用google令牌对用户进行身份验证并检索其google id,电子邮件等,而不是在周围发送普通的googleId。
为了保护我的API避免在开发过程中发生意外故障,我通常通过Soap UI或Assertible等服务创建一些功能测试。您可以在此处列出端点以及给定参数希望得到的响应。
我面临的问题是,如何通过测试以编程方式模拟Google登录? Facebook具有创建测试用户link的功能,但显然我无法在Google Docs中找到相同的解决方案。当然,我可以获得一个Google令牌并对其进行硬编码,但是它的有效期很短(几个小时)。
[node.js Google库中有一个线程,但是没有适当的解决方案:github thread。
如何绕过此问题?
为您的测试帐户请求刷新令牌。然后在每次测试运行开始时使用它来请求访问令牌。参见How do I authorise an app (web or installed) without user intervention?