-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support for typescript #10
Open
KellyBennett
wants to merge
2
commits into
PlayFab:main
Choose a base branch
from
KellyBennett:typescript_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*.js.map | ||
*.ts | ||
.git* | ||
.vscode | ||
local.settings.json | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
bin | ||
obj | ||
csx | ||
.vs | ||
edge | ||
Publish | ||
|
||
*.user | ||
*.suo | ||
*.cscfg | ||
*.Cache | ||
project.lock.json | ||
|
||
/packages | ||
/TestResults | ||
|
||
/tools/NuGet.exe | ||
/App_Data | ||
/secrets | ||
/data | ||
.secrets | ||
appsettings.json | ||
local.settings.json | ||
|
||
node_modules | ||
dist | ||
|
||
# Local python packages | ||
.python_packages/ | ||
|
||
# Python Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"recommendations": [ | ||
"ms-azuretools.vscode-azurefunctions" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Debug Tests", // This is the configuration name you will see in debug sidebar | ||
"type": "pwa-node", | ||
"resolveSourceMapLocations": [ | ||
"${workspaceFolder}/**", | ||
"!**/node_modules/**" | ||
], | ||
"request": "launch", | ||
"runtimeArgs": [ | ||
"--inspect-brk", | ||
"${workspaceRoot}/node_modules/jest/bin/jest.js", // Path to Jest | ||
"--runInBand", | ||
"-i" | ||
], | ||
"console": "internalConsole", | ||
"internalConsoleOptions": "openOnSessionStart", | ||
"sourceMaps": true | ||
}, | ||
{ | ||
"name": "Attach to Node Functions", | ||
"type": "node", | ||
"request": "attach", | ||
"port": 9229, | ||
"preLaunchTask": "func: host start" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"azureFunctions.deploySubpath": ".", | ||
"azureFunctions.postDeployTask": "npm install", | ||
"azureFunctions.projectLanguage": "TypeScript", | ||
"azureFunctions.projectRuntime": "~3", | ||
"debug.internalConsoleOptions": "neverOpen", | ||
"azureFunctions.preDeployTask": "npm prune", | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "func", | ||
"command": "host start", | ||
"problemMatcher": "$func-node-watch", | ||
"isBackground": true, | ||
"dependsOn": "npm build" | ||
}, | ||
{ | ||
"type": "shell", | ||
"label": "npm build", | ||
"command": "npm run build", | ||
"dependsOn": "npm install", | ||
"problemMatcher": "$tsc" | ||
}, | ||
{ | ||
"type": "shell", | ||
"label": "npm install", | ||
"command": "npm install" | ||
}, | ||
{ | ||
"type": "shell", | ||
"label": "npm prune", | ||
"command": "npm prune --production", | ||
"dependsOn": "npm build", | ||
"problemMatcher": [] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"authLevel": "function", | ||
"type": "httpTrigger", | ||
"route": "CloudScript/ExecuteFunction", | ||
"direction": "in", | ||
"name": "req", | ||
"methods": ["get", "post"] | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "res" | ||
} | ||
], | ||
"scriptFile": "../dist/ExecuteFunction/index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
import axios from 'axios'; | ||
import { gzip, ungzip } from 'node-gzip'; | ||
import functionToTest from './index'; | ||
|
||
const { runStubFunctionFromBindings, createHttpTrigger } = require('stub-azure-function-context'); | ||
|
||
// Mock response when fetching Profile from PlayFab | ||
const mockProfileSuccessResponse = { | ||
status: 200, | ||
statusText: 'OK', | ||
data: { | ||
code: 200, | ||
status: 'OK', | ||
data: { | ||
Profile: { | ||
Entity: { | ||
Id: '67BA331332AF6263', | ||
Type: 'title_player_account', | ||
TypeString: 'title_player_account', | ||
}, | ||
EntityChain: 'title_player_account!BDA2F45267E09A44/ALSLF/D99A2D9D9E5FDD94/67BCE31312AF6263/', | ||
VersionNumber: 0, | ||
Lineage: { | ||
NamespaceId: 'BDA2F96667E09A44', | ||
TitleId: 'asdfas', | ||
MasterPlayerAccountId: 'D99A2ASD9E5FDD94', | ||
TitlePlayerAccountId: '67BCE3131SFE263', | ||
}, | ||
Created: '2020-12-08T19:19:53.37Z', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const mockProfileUnauthorizedResponse = { | ||
status: 200, | ||
statusText: 'OK', | ||
data: { | ||
code: 401, | ||
status: 'Unauthorized', | ||
error: 'NotAuthenticated', | ||
errorCode: 1074, | ||
errorMessage: 'This API method does not allow anonymous callers.', | ||
}, | ||
}; | ||
|
||
// Mock response from your AzureFunction (the one refered to by FunctionName) | ||
const mockFunctionResponse = { | ||
status: 200, | ||
statusText: 'OK', | ||
data: {}, | ||
}; | ||
|
||
const defaultRequestBody = { | ||
FunctionName: 'RequestMatch', | ||
Entity: { | ||
Id: 'asldkf', | ||
Type: 'asdfasd', | ||
TypeString: 'asdfasldfj', | ||
}, | ||
}; | ||
|
||
const requestBodyWithoutEntity = { | ||
FunctionName: 'RequestMatch', | ||
}; | ||
|
||
const defaultHeaders = { | ||
'content-type': 'application/json; charset=utf-8', | ||
accept: '*/*', | ||
'accept-encoding': 'deflate, gzip', | ||
host: 'localhost:7071', | ||
'user-agent': 'Crimson/++UE4+Release-4.25-CL-0 Windows/10.0.18363.1.256.64bit', | ||
'content-length': '31', | ||
'x-playfabsdk': 'UE4MKPL-1.48.201014', | ||
'x-entitytoken': | ||
'M3x7ImkiOiIyMDIwLTEyLTA4VDAwOjMxOjAzLjMzMTIwMDhaIiwiaWRwIjoiQ3VzdG9tIiwiZSI6IjIwMjAtMTItMDlUMDA6MzE6MDMuMzMxMjAwOFoiLCJoIjoiMzI1NkUyMjI4MjVFQUNFNiIsInMiOiJjYWw3SzhKN3B1dUFleTNONjY2bXJ6Qk5zc1RnditDSHBKOHJGZ1FlbUw4PSIsImVjIjoidGl0bGVfcGxheWVyX2FjY291bnQhQkRBMkY5NjY2N0UwOUE0NC83RTNGQi8xOUVGMEMzQkQxNUMzMzQ3L0U4Q0Y2Qjg0RkQ2MEZBMDQvIiwiZWkiOiJFOENGNkI4NEZENjBGQTA0IiwiZXQiOiJ0aXRsFjfobow8ZXJfYWNjb3VudCJ9', | ||
}; | ||
|
||
const headersWithoutComrpression = { | ||
'content-type': 'application/json; charset=utf-8', | ||
accept: '*/*', | ||
host: 'localhost:7071', | ||
'user-agent': 'Crimson/++UE4+Release-4.25-CL-0 Windows/10.0.18363.1.256.64bit', | ||
'content-length': '31', | ||
'x-playfabsdk': 'UE4MKPL-1.48.201014', | ||
'x-entitytoken': | ||
'M3x7ImkiOiIyMDIwLTEyLTA4VDAwOjMxOjAzLjMzMTIwMDhaIiwiaWRwIjoiQ3VzdG9tIiwiZSI6IjIwMjAtMTItMDlUMDA6MzE6MDMuMzMxMjAwOFoiLCJoIjoiMzI1NkUyMjI4MjVFQUNFNiIsInMiOiJjYWw3SzhKN3B1dUFleTNONjY2bXJ6Qk5zc1RnditDSHBKOHJGZ1FlbUw4PSIsImVjIjoidGl0bGVfcGxheWVyX2FjY291bnQhQkRBMkY5NjY2N0UwOUE0NC83RTNGQi8xOUVGMEMzQkQxNUMzMzQ3L0U4Q0Y2Qjg0RkQ2MEZBMDQvIiwiZWkiOiJFOENGNkI4NEZENjBGQTA0IiwiZXQiOiJ0aXRsFjfobow8ZXJfYWNjb3VudCJ9', | ||
}; | ||
|
||
const headersWithoutEntityToken = { | ||
'content-type': 'application/json; charset=utf-8', | ||
accept: '*/*', | ||
'accept-encoding': 'deflate, gzip', | ||
host: 'localhost:7071', | ||
'user-agent': 'Crimson/++UE4+Release-4.25-CL-0 Windows/10.0.18363.1.256.64bit', | ||
'content-length': '31', | ||
'x-playfabsdk': 'UE4MKPL-1.48.201014', | ||
}; | ||
|
||
interface BootstrapParams { | ||
body?: object; | ||
headers?: object; | ||
} | ||
|
||
const bootstrappedFunction = async ({ body = defaultRequestBody, headers = defaultHeaders }: BootstrapParams = {}) => { | ||
return runStubFunctionFromBindings( | ||
functionToTest, | ||
[ | ||
{ | ||
type: 'httpTrigger', | ||
name: 'req', | ||
direction: 'in', | ||
data: createHttpTrigger('POST', 'http://localhost:7071/CloudScript/ExecuteFunction', headers, {}, body), | ||
}, | ||
{ type: 'http', name: 'res', direction: 'out' }, | ||
], | ||
new Date(), | ||
); | ||
}; | ||
|
||
describe('ExecuteFunction', () => { | ||
process.env.PLAYFAB_TITLE_ID = 'TESTID'; | ||
|
||
it('Responds successfully with valid params', async () => { | ||
const mockedAxios = jest.spyOn(axios, 'post'); | ||
mockedAxios.mockResolvedValueOnce(mockProfileSuccessResponse).mockResolvedValueOnce(mockFunctionResponse); | ||
|
||
const context = await bootstrappedFunction(); | ||
expect(context).toHaveProperty('res.status', 200); | ||
|
||
mockedAxios.mockRestore(); | ||
}); | ||
|
||
it('Calls playfab to fetch a profile', async () => { | ||
const mockedAxios = jest.spyOn(axios, 'post'); | ||
mockedAxios.mockResolvedValueOnce(mockProfileSuccessResponse).mockResolvedValueOnce(mockFunctionResponse); | ||
|
||
await bootstrappedFunction(); | ||
expect(mockedAxios).toHaveBeenCalledWith( | ||
'/Profile/GetProfile', | ||
{ Entity: { Id: 'asldkf', Type: 'asdfasd', TypeString: 'asdfasd' } }, | ||
{ | ||
baseURL: 'https://TESTID.playfabapi.com', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-EntityToken': | ||
'M3x7ImkiOiIyMDIwLTEyLTA4VDAwOjMxOjAzLjMzMTIwMDhaIiwiaWRwIjoiQ3VzdG9tIiwiZSI6IjIwMjAtMTItMDlUMDA6MzE6MDMuMzMxMjAwOFoiLCJoIjoiMzI1NkUyMjI4MjVFQUNFNiIsInMiOiJjYWw3SzhKN3B1dUFleTNONjY2bXJ6Qk5zc1RnditDSHBKOHJGZ1FlbUw4PSIsImVjIjoidGl0bGVfcGxheWVyX2FjY291bnQhQkRBMkY5NjY2N0UwOUE0NC83RTNGQi8xOUVGMEMzQkQxNUMzMzQ3L0U4Q0Y2Qjg0RkQ2MEZBMDQvIiwiZWkiOiJFOENGNkI4NEZENjBGQTA0IiwiZXQiOiJ0aXRsFjfobow8ZXJfYWNjb3VudCJ9', | ||
}, | ||
}, | ||
); | ||
mockedAxios.mockRestore(); | ||
}); | ||
|
||
it('Calls local Azure Function', async () => { | ||
const mockedAxios = jest.spyOn(axios, 'post'); | ||
mockedAxios.mockResolvedValueOnce(mockProfileSuccessResponse).mockResolvedValueOnce(mockFunctionResponse); | ||
|
||
await bootstrappedFunction(); | ||
|
||
expect(mockedAxios).toHaveBeenCalledWith( | ||
'/api/RequestMatch', | ||
'{"TitleAuthenticationContext":{"Id":"TESTID","EntityToken":"M3x7ImkiOiIyMDIwLTEyLTA4VDAwOjMxOjAzLjMzMTIwMDhaIiwiaWRwIjoiQ3VzdG9tIiwiZSI6IjIwMjAtMTItMDlUMDA6MzE6MDMuMzMxMjAwOFoiLCJoIjoiMzI1NkUyMjI4MjVFQUNFNiIsInMiOiJjYWw3SzhKN3B1dUFleTNONjY2bXJ6Qk5zc1RnditDSHBKOHJGZ1FlbUw4PSIsImVjIjoidGl0bGVfcGxheWVyX2FjY291bnQhQkRBMkY5NjY2N0UwOUE0NC83RTNGQi8xOUVGMEMzQkQxNUMzMzQ3L0U4Q0Y2Qjg0RkQ2MEZBMDQvIiwiZWkiOiJFOENGNkI4NEZENjBGQTA0IiwiZXQiOiJ0aXRsFjfobow8ZXJfYWNjb3VudCJ9"},"CallerEntityProfile":{"Entity":{"Id":"67BA331332AF6263","Type":"title_player_account","TypeString":"title_player_account"},"EntityChain":"title_player_account!BDA2F45267E09A44/ALSLF/D99A2D9D9E5FDD94/67BCE31312AF6263/","VersionNumber":0,"Lineage":{"NamespaceId":"BDA2F96667E09A44","TitleId":"asdfas","MasterPlayerAccountId":"D99A2ASD9E5FDD94","TitlePlayerAccountId":"67BCE3131SFE263"},"Created":"2020-12-08T19:19:53.37Z"}}', | ||
{ baseURL: 'http://localhost:7071', headers: { 'Content-Type': 'application/json' } }, | ||
); | ||
mockedAxios.mockRestore(); | ||
}); | ||
|
||
it('Returns a gzipped response body', async () => { | ||
const mockedAxios = jest.spyOn(axios, 'post'); | ||
mockedAxios.mockResolvedValueOnce(mockProfileSuccessResponse).mockResolvedValueOnce(mockFunctionResponse); | ||
|
||
const response = await bootstrappedFunction(); | ||
expect(Buffer.isBuffer(response.res.body)).toBe(true); | ||
ungzip(response.res.body) | ||
.then((uncompressed) => { | ||
return gzip(uncompressed); | ||
}) | ||
.then((compressed) => { | ||
expect(compressed).toEqual(response.res.body); | ||
}); | ||
|
||
mockedAxios.mockRestore(); | ||
}); | ||
|
||
it('Respondes with a full "ExecuteFunctionResult"', async () => { | ||
const mockedAxios = jest.spyOn(axios, 'post'); | ||
mockedAxios.mockResolvedValueOnce(mockProfileSuccessResponse).mockResolvedValueOnce(mockFunctionResponse); | ||
|
||
const response = await bootstrappedFunction(); | ||
const unzippedResponseBody = await ungzip(response.res.body); | ||
const responseString = unzippedResponseBody.toString(); | ||
const responseBody = JSON.parse(responseString); | ||
expect(responseBody).toHaveProperty('data.ExecutionTimeMilliseconds'); | ||
expect(responseBody).toHaveProperty('data.FunctionName'); | ||
expect(responseBody).toHaveProperty('data.FunctionResult'); | ||
expect(responseBody).toHaveProperty('data.FunctionResultTooLarge'); | ||
|
||
mockedAxios.mockRestore(); | ||
}); | ||
|
||
it('Does not gzip the result if gzip header is not present', async () => { | ||
const mockedAxios = jest.spyOn(axios, 'post'); | ||
mockedAxios.mockResolvedValueOnce(mockProfileSuccessResponse).mockResolvedValueOnce(mockFunctionResponse); | ||
|
||
const response = await bootstrappedFunction({ headers: headersWithoutComrpression }); | ||
expect(Buffer.isBuffer(response.res.body)).toBe(true); | ||
expect(async () => { | ||
await ungzip(response.res.body); | ||
}) | ||
.rejects // https://stackoverflow.com/a/61214808 | ||
.toThrowError('Error: incorrect header check'); | ||
|
||
mockedAxios.mockRestore(); | ||
}); | ||
|
||
it('Returns a 401 when no Caller Entity Token header is present', async () => { | ||
const mockedAxios = jest.spyOn(axios, 'post'); | ||
mockedAxios.mockResolvedValueOnce(mockProfileUnauthorizedResponse).mockResolvedValueOnce(mockFunctionResponse); | ||
const response = await bootstrappedFunction({ headers: headersWithoutEntityToken }); | ||
expect(response).toHaveProperty('res.status', 401); | ||
mockedAxios.mockRestore(); | ||
}); | ||
|
||
it('Returns a 200 when Entity is present in the request body', async () => { | ||
const mockedAxios = jest.spyOn(axios, 'post'); | ||
mockedAxios.mockResolvedValueOnce(mockProfileSuccessResponse).mockResolvedValueOnce(mockFunctionResponse); | ||
const response = await bootstrappedFunction({ body: requestBodyWithoutEntity }); | ||
expect(response).toHaveProperty('res.status', 200); | ||
mockedAxios.mockRestore(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume if no Entity is provided in the request body, we still want the call to succeed? Just calling this out here because I'm not 100% what circumstances lead to this happening, or what the expected behavior is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if no entity is provided in the body, the entity in the entity token is used. Passing an entity in the body allows a title to execute a function as a player, for example. But if the title just wanted to execute a function as itself, it can just pass its entity token as normal and not specify an entity in the request body. Most functions are called by players, but we have some cases where customers are calling using a title entity token (see #9)