Skip to content

Commit

Permalink
Added nodejs integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeManSu committed Jul 12, 2024
1 parent ecc70ff commit ec7cde1
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 6 deletions.
19 changes: 18 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,24 @@
"caughtErrorsIgnorePattern": "^_"
}
]
}
},
"overrides": [
{
"files": [
"*.js"
],
"rules": {
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-floating-promises": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-unsafe-return": "off"

}
}
]
},
"dependencies": {
"@metacall/protocol": "^0.1.26",
Expand Down
10 changes: 10 additions & 0 deletions test/data/nodejs-base-app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node

function isPalindrome(str) {
const cleanedStr = String(str)
.replace(/[^a-zA-Z0-9]/g, '')
.toLowerCase();
return cleanedStr === cleanedStr.split('').reverse().join('');
}

module.exports = { isPalindrome };
5 changes: 5 additions & 0 deletions test/data/nodejs-base-app/metacall.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"language_id": "node",
"path": ".",
"scripts": ["index.js"]
}
5 changes: 5 additions & 0 deletions test/data/nodejs-dependency-app/metacall.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"language_id": "node",
"path": ".",
"scripts": ["middleware.js"]
}
45 changes: 45 additions & 0 deletions test/data/nodejs-dependency-app/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env node

const jwt = require('jsonwebtoken');
const secret = 'secret';

// Mock user validation (DB)
function validUser(user, password) {
return user === 'viferga' && password === '123';
}

// Sign in function that returns a token if login is valid
function signin(user, password) {
if (validUser(user, password)) return jwt.sign({ user, password }, secret);

return false;
}

// Reverse string function
function reverse({ str }) {
return str.split('').reverse().join('');
}

// Sum function
function sum({ a, b }) {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
return a + b;
}

// Middleware for verifying the token
function middleware(f) {
return function (token, args) {
try {
return jwt.verify(token, secret) ? f(args) : 'Invalid token';
} catch (e) {
return e.message;
}
};
}

// Export sign in without middleware and reverse and sum with it
module.exports = {
signin,
reverse: middleware(reverse),
sum: middleware(sum)
};
87 changes: 87 additions & 0 deletions test/data/nodejs-dependency-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/data/nodejs-dependency-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "auth-middleware",
"version": "0.1.0",
"dependencies": {
"jsonwebtoken": "^9.0.0"
}
}
46 changes: 43 additions & 3 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ RETRY_COUNT=0
# FaaS base URL
BASE_URL="http://localhost:9000"

# #function to check readiness
# Function to check readiness
function check_readiness() {
local status_code
status_code=$(curl -s -o /dev/null -w "%{http_code}" $BASE_URL/readiness)
Expand Down Expand Up @@ -129,10 +129,50 @@ function test_python_dependency_app() {
[[ $(curl -s $url/fetchJoke) == *"setup"* && $(curl -s $url/fetchJoke) == *"punchline"* ]] || exit 1
}

# Run tests without dependencies
# Test function for nodejs-base-app
function test_nodejs_app() {
local url=$1

local response1
response1=$(curl -s -X POST -H "Content-Type: application/json" -d '{"params":["madam"]}' $url/isPalindrome)
[[ $response1 == "true" ]] || exit 1

local response2
response2=$(curl -s -X POST -H "Content-Type: application/json" -d '{"params":["world"]}' $url/isPalindrome)
[[ $response2 == "false" ]] || exit 1
}

# Test function for nodejs-dependency-app
function test_nodejs_dependency_app() {
local url=$1

local signin_response
signin_response=$(curl -s -X POST -H "Content-Type: application/json" -d '{"user":"viferga","password":"123"}' $url/signin)

local token
token=$(echo $signin_response | sed 's/^"\(.*\)"$/\1/')

if [[ -z "$token" ]]; then
echo "Failed to extract token"
exit 1
fi

local reverse_response
reverse_response=$(curl -s -X POST -H "Content-Type: application/json" -d '{"token":"'"$token"'","args":{"str":"hello"}}' $url/reverse)
[[ $reverse_response = '"olleh"' ]] || exit 1

local sum_response
sum_response=$(curl -s -X POST -H "Content-Type: application/json" -d '{"token":"'"$token"'","args":{"a":1,"b":2}}' $url/sum)
[[ $sum_response = 3 ]] || exit 1
}

# Run tests
run_tests "nodejs-base-app" test_nodejs_app
run_tests "python-base-app" test_python_base_app
if [[ "${TEST_FAAS_DEPENDENCY_DEPLOY}" == "true" ]]; then
if [[ "${TEST_FAAS_DEPENDENCY_DEPLOY}" == "false" ]]; then
run_tests "python-dependency-app" test_python_dependency_app
run_tests "nodejs-dependency-app" test_nodejs_dependency_app

fi

echo "Integration tests passed without errors."
8 changes: 6 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
"outDir": "dist",
"esModuleInterop": true
},
"include": ["types/*.ts", "src/*.ts", "src/**/*.ts"]
}
"include": [
"types/*.ts",
"src/**/*.ts",
"test/**/*.js"
]
}

0 comments on commit ec7cde1

Please sign in to comment.