Skip to content

Commit

Permalink
Merge branch 'main' into vkarpov15/remove-email
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Aug 23, 2024
2 parents a9e4e11 + ef35b7d commit 68cdfb2
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ module.exports = {
rules: {
'comma-dangle': 'off',
'space-before-function-paren': 'off',
'eol-last': 'warn'
'eol-last': 'warn',
},
};
19 changes: 19 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,22 @@ jobs:

- run: npm install
- run: npm run lint

prettier:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
node: [18]
os: [ubuntu-20.04]
name: Prettier
steps:
- uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # v3

- name: Setup node
uses: actions/setup-node@5b52f097d36d4b0b2f94ed6de710023fbb8b2236 # v3.1.0
with:
node-version: ${{ matrix.node }}

- run: npm install
- run: npm run prettier -- --check
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"printWidth": 80,
"singleQuote": true
}
7 changes: 4 additions & 3 deletions oso.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const assert = require('assert');
const apiKey = process.env.OSO_CLOUD_API_KEY;
assert.ok(apiKey, 'Must set OSO_CLOUD_API_KEY environment variable');

const oso = process.env.NODE_ENV === 'test'
? new Oso('http://localhost:8080', apiKey)
: new Oso('https://cloud.osohq.com', apiKey, { debug: { print: true } });
const oso =
process.env.NODE_ENV === 'test'
? new Oso('http://localhost:8080', apiKey)
: new Oso('https://cloud.osohq.com', apiKey, { debug: { print: true } });

module.exports = oso;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"tailwind:watch": "tailwindcss -o ./public/style.css --watch",
"dev": "next dev",
"build": "next build",
"prettier": "prettier --write ./src ./test ./backend ./db ./pages ./index.js ./build.js ./levels.js ./oso.js ./webpack.config.js",
"start": "next start",
"test": "mocha ./test/*.test.js"
},
Expand All @@ -32,6 +33,7 @@
"@mongoosejs/studio": "0.0.22",
"dotenv": "16.4.5",
"mocha": "10.4.0",
"prettier": "3.3.3",
"sinon": "18.0.0"
}
}
11 changes: 8 additions & 3 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

const axios = require('axios');

const client = typeof API_URL === 'undefined' ? axios.create() : axios.create({
baseURL: API_URL
});
let client = null;
if (typeof API_URL === 'undefined') {
client = axios.create();
} else {
client = axios.create({
baseURL: API_URL,
});
}

module.exports = client;
4 changes: 2 additions & 2 deletions src/components.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

// This file is auto-generated. Do not modify this file directly.
// This file is auto-generated. Do not modify this file directly.
exports['add-role-fact'] = require('./add-role-fact/add-role-fact.js');
exports['app-component'] = require('./app-component/app-component.js');
exports['async-button'] = require('./async-button/async-button.js');
Expand All @@ -11,4 +11,4 @@ exports['modal'] = require('./modal/modal.js');
exports['oso-footer'] = require('./oso-footer/oso-footer.js');
exports['scorecard'] = require('./scorecard/scorecard.js');
exports['share'] = require('./share/share.js');
exports['splash-screen'] = require('./splash-screen/splash-screen.js');
exports['splash-screen'] = require('./splash-screen/splash-screen.js');
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const components = require('./components');
const levels = require('../levels');
const vanillatoasts = require('vanillatoasts');

const sessionId = window.localStorage.getItem('_gitclubGameSession')
|| [...Array(30)].map(() => Math.random().toString(36)[2]).join('');
const sessionId =
window.localStorage.getItem('_gitclubGameSession') ||
[...Array(30)].map(() => Math.random().toString(36)[2]).join('');
window.localStorage.setItem('_gitclubGameSession', sessionId);

window.setLevel = require('./_methods/setLevel');
Expand Down
33 changes: 23 additions & 10 deletions src/level/level.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,21 @@ module.exports = (app) =>
},
deleteInProgress: false,
showDeleteAllModal: false,
highlightedCode: []
highlightedCode: [],
}),
template,
computed: {
polarCode() {
return this.highlightedCode.map(line => {
return line.map(chunk => `<span style="${stringifyStyle(chunk.style)}">${chunk.content}</span>`).join('');
}).join('\n');
return this.highlightedCode
.map((line) => {
return line
.map(
(chunk) =>
`<span style="${stringifyStyle(chunk.style)}">${chunk.content}</span>`,
)
.join('');
})
.join('\n');
},
allResources() {
let ret = ['Organization', 'Repository', 'User'];
Expand Down Expand Up @@ -177,9 +184,13 @@ module.exports = (app) =>
this.roleFact.role = null;
}
},
'state.currentLevel': async function(currentLevel) {
'state.currentLevel': async function (currentLevel) {
this.highlightedCode = [];
const result = await lighter.highlight(currentLevel.polarCode, 'polar', 'github-light');
const result = await lighter.highlight(
currentLevel.polarCode,
'polar',
'github-light',
);
this.highlightedCode = result.lines;
},
},
Expand Down Expand Up @@ -219,7 +230,7 @@ module.exports = (app) =>
resourceType,
...this.attributeFact,
});

this.attributeFact = {
resourceId: null,
attribute: null,
Expand Down Expand Up @@ -304,7 +315,9 @@ function stringifyStyle(obj) {
if (obj == null) {
return '';
}
return Object.entries(obj).map(([key, value]) => {
return `${key}:${value}`;
}).join(';');
return Object.entries(obj)
.map(([key, value]) => {
return `${key}:${value}`;
})
.join(';');
}
75 changes: 44 additions & 31 deletions test/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,30 @@ const oso = require('../oso');
const levels = require('../levels');
const { renderToString } = require('vue/server-renderer');

const policy = require('fs').readFileSync(`${__dirname}/../src/policy.polar`, 'utf8');
const policy = require('fs').readFileSync(
`${__dirname}/../src/policy.polar`,
'utf8',
);

describe('e2e test', function() {
describe('e2e test', function () {
let appInstance = null;
let server = null;
let state = null;

before(async function() {
before(async function () {
await connect();
await Player.deleteMany({});
});

before(async function() {
before(async function () {
state = reactive({
sessionId: 'e2eTest',
level: 0,
currentLevel: levels[0],
facts: [],
results: [],
constraints: levels[0].constraints,
showNextLevelButton: true
showNextLevelButton: true,
});
const app = createSSRApp({
data: () => ({}),
Expand All @@ -43,24 +46,24 @@ describe('e2e test', function() {
},
setup() {
provide('state', state);
}
},
});
for (const component of Object.values(components)) {
component(app);
}
await renderToString(app);
});

before(async function() {
before(async function () {
await oso.policy(policy);

const app = express();
app.use(express.json());
for (const endpoint of Object.keys(backend)) {
app.all(`/api/${kebabize(endpoint)}`, function(req, res) {
app.all(`/api/${kebabize(endpoint)}`, function (req, res) {
backend[endpoint]({ ...req.body, ...req.query })
.then(result => res.json(result))
.catch(err => {
.then((result) => res.json(result))
.catch((err) => {
console.error(err);
res.status(500).json({ message: err.message });
});
Expand All @@ -69,17 +72,19 @@ describe('e2e test', function() {
server = await app.listen(3000);
});

after(async function() {
after(async function () {
await server.close();
});

it('can complete first level', async function() {
it('can complete first level', async function () {
this.timeout(10000);

const appComponent = appInstance.$options.$children[0];

// Complete splash screen
const splashScreenComponent = appComponent.$options.$children.find(el => el.$options.name === 'splash-screen');
const splashScreenComponent = appComponent.$options.$children.find(
(el) => el.$options.name === 'splash-screen',
);
splashScreenComponent.name = 'John Smith';
await splashScreenComponent.startGame();
assert.strictEqual(state.level, 1);
Expand All @@ -90,20 +95,24 @@ describe('e2e test', function() {
action: 'read',
resourceType: 'Organization',
resourceId: 'osohq',
pass: false
pass: false,
},
{
userId: 'Anthony',
action: 'add_member',
resourceType: 'Organization',
resourceId: 'osohq',
pass: false
}
pass: false,
},
]);

// Add first fact
const levelComponent = appComponent.$options.$children.find(el => el.$options.name === 'level');
const addRoleFactComponent = levelComponent.$options.$children.find(el => el.$options.name === 'add-role-fact');
const levelComponent = appComponent.$options.$children.find(
(el) => el.$options.name === 'level',
);
const addRoleFactComponent = levelComponent.$options.$children.find(
(el) => el.$options.name === 'add-role-fact',
);

addRoleFactComponent.userId = 'Alice';
addRoleFactComponent.role = 'admin';
Expand All @@ -118,15 +127,15 @@ describe('e2e test', function() {
action: 'read',
resourceType: 'Organization',
resourceId: 'osohq',
pass: true
pass: true,
},
{
userId: 'Anthony',
action: 'add_member',
resourceType: 'Organization',
resourceId: 'osohq',
pass: false
}
pass: false,
},
]);
assert.ok(!state.showNextLevelButton);

Expand All @@ -144,15 +153,15 @@ describe('e2e test', function() {
action: 'read',
resourceType: 'Organization',
resourceId: 'osohq',
pass: true
pass: true,
},
{
userId: 'Anthony',
action: 'add_member',
resourceType: 'Organization',
resourceId: 'osohq',
pass: true
}
pass: true,
},
]);
assert.ok(state.showNextLevelButton);

Expand All @@ -165,32 +174,36 @@ describe('e2e test', function() {
action: 'read',
resourceType: 'Organization',
resourceId: 'osohq',
pass: false
pass: false,
},
{
userId: 'Bob',
action: 'add_member',
resourceType: 'Organization',
resourceId: 'osohq',
pass: false
pass: false,
},
{
userId: 'Bill',
action: 'read',
resourceType: 'Organization',
resourceId: 'osohq',
pass: false
pass: false,
},
{
userId: 'Bill',
action: 'add_member',
resourceType: 'Organization',
resourceId: 'osohq',
pass: true,
shouldFail: true
}
shouldFail: true,
},
]);
});
});

const kebabize = (str) => str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? '-' : '') + $.toLowerCase());
const kebabize = (str) =>
str.replace(
/[A-Z]+(?![a-z])|[A-Z]/g,
($, ofs) => (ofs ? '-' : '') + $.toLowerCase(),
);

0 comments on commit 68cdfb2

Please sign in to comment.