Skip to content

Commit

Permalink
Merge pull request #339 from sinamics/generate_token_userapi
Browse files Browse the repository at this point in the history
Generating an API Token for New Users via REST API
  • Loading branch information
sinamics authored Mar 13, 2024
2 parents 64e6495 + 793c6ca commit b2bdae3
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 211 deletions.
82 changes: 10 additions & 72 deletions docs/docs/Rest Api/Personal/_source/network.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,11 @@ paths:
example:
$ref: '../../_example/NetworkExample.yml#/NetworkArrayExample'
401:
description: Unauthorized
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/Unauthorized.yml#/Unauthorized'
429:
description: Rate limit exceeded
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/RateLimitExceeded.yml#/RateLimitExceeded'
500:
description: Internal server error
content:
application/json:
schema:
type: object
properties:
message:
type: string
$ref: '../../_http_responses/InternalServerError.yml#/InternalServerError'

/network/{networkId}:
get:
Expand Down Expand Up @@ -93,32 +72,12 @@ paths:
example:
$ref: '../../_example/NetworkExample.yml#/NetworkExample'
401:
description: Unauthorized
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/Unauthorized.yml#/Unauthorized'
429:
description: Rate limit exceeded
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/RateLimitExceeded.yml#/RateLimitExceeded'
500:
description: Internal server error
content:
application/json:
schema:
type: object
properties:
message:
type: string
$ref: '../../_http_responses/InternalServerError.yml#/InternalServerError'

# POST /network
post:
summary: Create New Network
Expand Down Expand Up @@ -155,29 +114,8 @@ paths:
$ref: '../../_example/NetworkExample.yml#/NetworkExample'

401:
description: Unauthorized
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/Unauthorized.yml#/Unauthorized'
429:
description: Rate limit exceeded
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/RateLimitExceeded.yml#/RateLimitExceeded'
500:
description: Internal server error
content:
application/json:
schema:
type: object
properties:
message:
type: string
$ref: '../../_http_responses/InternalServerError.yml#/InternalServerError'
83 changes: 11 additions & 72 deletions docs/docs/Rest Api/Personal/_source/networkMember.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,12 @@ paths:
example:
$ref: '../../_example/NetworkMemberExample.yml#/NetworkArrayMemberExample'
401:
description: Unauthorized
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/Unauthorized.yml#/Unauthorized'
429:
description: Rate limit exceeded
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/RateLimitExceeded.yml#/RateLimitExceeded'
500:
description: Internal server error
content:
application/json:
schema:
type: object
properties:
message:
type: string
$ref: '../../_http_responses/InternalServerError.yml#/InternalServerError'

/network/{networkId}/member/{memberId}:
post:
summary: Modify a network member
Expand Down Expand Up @@ -115,32 +95,12 @@ paths:
example:
$ref: '../../_example/NetworkMemberExample.yml#/NetworkArrayMemberExample'
401:
description: Unauthorized
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/Unauthorized.yml#/Unauthorized'
429:
description: Rate limit exceeded
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/RateLimitExceeded.yml#/RateLimitExceeded'
500:
description: Internal server error
content:
application/json:
schema:
type: object
properties:
message:
type: string
$ref: '../../_http_responses/InternalServerError.yml#/InternalServerError'

delete:
summary: Delete a network member
description: This action flags the member as deleted, revokes their authorization, and updates their status in the database. Consequently, the member will be moved to the "Stashed Members" list, indicating their altered status.
Expand Down Expand Up @@ -168,29 +128,8 @@ paths:
example:
$ref: '../../_example/NetworkExample.yml#/NetworkArrayExample'
401:
description: Unauthorized
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/Unauthorized.yml#/Unauthorized'
429:
description: Rate limit exceeded
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/RateLimitExceeded.yml#/RateLimitExceeded'
500:
description: Internal server error
content:
application/json:
schema:
type: object
properties:
message:
type: string
$ref: '../../_http_responses/InternalServerError.yml#/InternalServerError'
44 changes: 17 additions & 27 deletions docs/docs/Rest Api/Personal/_source/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,18 @@ paths:
Must be in ISO 8601 format (e.g. 2023-11-03T00:00:00.000Z).
Not applicable if the user is an admin (first user). Admin accounts never expire.
generateApiToken:
type: boolean
nullable: true
description: |
If set to true, the response will include an API token with a 24-hour validity period, applicable for both Personal and Organization API access.
example:
email: "[email protected]"
password: "strong_password"
name: "Ztnet User"
expiresAt: "2023-11-03T00:00:00.000Z"
generateApiToken: true
# parameters:
# - name: x-ztnet-auth
# in: header
Expand All @@ -88,39 +95,22 @@ paths:
type: string
expiresAt:
type: string
apiToken:
type: string
example:
id: "12345"
email: "[email protected]"
name: "Test User"
id: "abcd1234"
email: "[email protected]"
name: "John Doe"
expiresAt: "2023-10-28T00:00:00Z"
apiToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9ihuihfds78tewgyre67dtfeswr7gyu3q8gfrv78ewrtgf8wrb98SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"

401:
description: Unauthorized
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/Unauthorized.yml#/Unauthorized'
429:
description: Rate limit exceeded
content:
application/json:
schema:
type: object
properties:
error:
type: string
$ref: '../../_http_responses/RateLimitExceeded.yml#/RateLimitExceeded'
500:
description: Internal server error
content:
application/json:
schema:
type: object
properties:
message:
type: string
$ref: '../../_http_responses/InternalServerError.yml#/InternalServerError'

components:
operationId: postNewUser
securitySchemes:
Expand Down
6 changes: 6 additions & 0 deletions src/pages/api/__tests__/v1/user/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ describe("createUserHandler", () => {
it("should create a user successfully", async () => {
prisma.user.count = jest.fn().mockResolvedValue(0);

// mock prisma transaction
prisma.$transaction = jest.fn().mockResolvedValue({ id: "newUserId" });

// Mock the decryption to fail
(encryptionModule.decryptAndVerifyToken as jest.Mock).mockResolvedValue({
userId: "userId",
Expand Down Expand Up @@ -79,6 +82,9 @@ describe("createUserHandler", () => {
it("should respond 401 when decryptAndVerifyToken fails", async () => {
prisma.user.count = jest.fn().mockResolvedValue(1);

// mock prisma transaction
prisma.$transaction = jest.fn().mockResolvedValue({ id: "newUserId" });

// Mock the decryption to fail
(encryptionModule.decryptAndVerifyToken as jest.Mock).mockRejectedValue(
new Error("Invalid token"),
Expand Down
Loading

0 comments on commit b2bdae3

Please sign in to comment.