|
|
|
@ -16,7 +16,10 @@ const supertest = require("supertest") |
|
|
|
const { cleanup } = require("../../utilities/fileSystem") |
|
|
|
const { Cookies } = require("@budibase/auth").constants |
|
|
|
const { jwt } = require("@budibase/auth").auth |
|
|
|
const { StaticDatabases } = require("@budibase/auth").db |
|
|
|
const CouchDB = require("../../db") |
|
|
|
|
|
|
|
const GLOBAL_USER_ID = "us_uuid1" |
|
|
|
const EMAIL = "babs@babs.com" |
|
|
|
const PASSWORD = "babs_password" |
|
|
|
|
|
|
|
@ -57,7 +60,27 @@ class TestConfiguration { |
|
|
|
return request.body |
|
|
|
} |
|
|
|
|
|
|
|
async globalUser(id = GLOBAL_USER_ID, builder = true) { |
|
|
|
const db = new CouchDB(StaticDatabases.GLOBAL.name) |
|
|
|
let existing |
|
|
|
try { |
|
|
|
existing = await db.get(id) |
|
|
|
} catch (err) { |
|
|
|
existing = {} |
|
|
|
} |
|
|
|
const user = { |
|
|
|
_id: id, |
|
|
|
...existing, |
|
|
|
roles: {}, |
|
|
|
} |
|
|
|
if (builder) { |
|
|
|
user.builder = { global: true } |
|
|
|
} |
|
|
|
await db.put(user) |
|
|
|
} |
|
|
|
|
|
|
|
async init(appName = "test_application") { |
|
|
|
await this.globalUser() |
|
|
|
return this.createApp(appName) |
|
|
|
} |
|
|
|
|
|
|
|
@ -69,17 +92,14 @@ class TestConfiguration { |
|
|
|
} |
|
|
|
|
|
|
|
defaultHeaders() { |
|
|
|
const user = { |
|
|
|
userId: "ro_ta_user_us_uuid1", |
|
|
|
builder: { |
|
|
|
global: true, |
|
|
|
}, |
|
|
|
const auth = { |
|
|
|
userId: GLOBAL_USER_ID, |
|
|
|
} |
|
|
|
const app = { |
|
|
|
roleId: BUILTIN_ROLE_IDS.BUILDER, |
|
|
|
appId: this.appId, |
|
|
|
} |
|
|
|
const authToken = jwt.sign(user, env.JWT_SECRET) |
|
|
|
const authToken = jwt.sign(auth, env.JWT_SECRET) |
|
|
|
const appToken = jwt.sign(app, env.JWT_SECRET) |
|
|
|
const headers = { |
|
|
|
Accept: "application/json", |
|
|
|
@ -104,14 +124,18 @@ class TestConfiguration { |
|
|
|
return headers |
|
|
|
} |
|
|
|
|
|
|
|
async roleHeaders(email = EMAIL, roleId = BUILTIN_ROLE_IDS.ADMIN) { |
|
|
|
async roleHeaders({ |
|
|
|
email = EMAIL, |
|
|
|
roleId = BUILTIN_ROLE_IDS.ADMIN, |
|
|
|
builder = false, |
|
|
|
}) { |
|
|
|
let user |
|
|
|
try { |
|
|
|
user = await this.createUser(email, PASSWORD, roleId) |
|
|
|
} catch (err) { |
|
|
|
// allow errors here
|
|
|
|
} |
|
|
|
return this.login(email, PASSWORD, { roleId, userId: user._id }) |
|
|
|
return this.login(email, PASSWORD, { roleId, userId: user._id, builder }) |
|
|
|
} |
|
|
|
|
|
|
|
async createApp(appName) { |
|
|
|
@ -282,7 +306,9 @@ class TestConfiguration { |
|
|
|
password = PASSWORD, |
|
|
|
roleId = BUILTIN_ROLE_IDS.POWER |
|
|
|
) { |
|
|
|
return this._req( |
|
|
|
const globalId = `us_${Math.random()}` |
|
|
|
await this.globalUser(globalId, roleId === BUILTIN_ROLE_IDS.BUILDER) |
|
|
|
const user = await this._req( |
|
|
|
{ |
|
|
|
email, |
|
|
|
password, |
|
|
|
@ -291,28 +317,34 @@ class TestConfiguration { |
|
|
|
null, |
|
|
|
controllers.user.createMetadata |
|
|
|
) |
|
|
|
return { |
|
|
|
...user, |
|
|
|
globalId, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async login(email, password, { roleId, userId } = {}) { |
|
|
|
if (!roleId) { |
|
|
|
roleId = BUILTIN_ROLE_IDS.BUILDER |
|
|
|
} |
|
|
|
async login(email, password, { roleId, userId, builder } = {}) { |
|
|
|
roleId = !roleId ? BUILTIN_ROLE_IDS.BUILDER : roleId |
|
|
|
userId = !userId ? `us_uuid1` : userId |
|
|
|
if (!this.request) { |
|
|
|
throw "Server has not been opened, cannot login." |
|
|
|
} |
|
|
|
// make sure the user exists in the global DB
|
|
|
|
if (roleId !== BUILTIN_ROLE_IDS.PUBLIC) { |
|
|
|
await this.globalUser(userId, builder) |
|
|
|
} |
|
|
|
if (!email || !password) { |
|
|
|
await this.createUser() |
|
|
|
} |
|
|
|
// have to fake this
|
|
|
|
const user = { |
|
|
|
userId: userId || `us_uuid1`, |
|
|
|
email: email || EMAIL, |
|
|
|
const auth = { |
|
|
|
userId, |
|
|
|
} |
|
|
|
const app = { |
|
|
|
roleId: roleId, |
|
|
|
appId: this.appId, |
|
|
|
} |
|
|
|
const authToken = jwt.sign(user, env.JWT_SECRET) |
|
|
|
const authToken = jwt.sign(auth, env.JWT_SECRET) |
|
|
|
const appToken = jwt.sign(app, env.JWT_SECRET) |
|
|
|
|
|
|
|
// returning necessary request headers
|
|
|
|
|