-
Notifications
You must be signed in to change notification settings - Fork 11
#8 Creating database and user & granting user privileges to database #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e582957
postgres start, create, delete, close database/jest
albertoelopez 97fddf2
update npm modules and prevent undefined values from running
albertoelopez a670e2b
updated error management and package.json file
albertoelopez f2e2a73
updated tests
albertoelopez 4b0c933
fix mistake
albertoelopez 1b31f5e
got rid of try/catch for start and close connection
albertoelopez c360d87
deleted async from start and close connection
albertoelopez ab9c8ba
added tests to check for undefined inputs and deleted unecessary asse…
albertoelopez 9d5ee02
fixed comments for tests to make them more explicit
albertoelopez 7204f88
update description of catch block testing
albertoelopez 3a651d9
fixed last issue on line 35, test was for deltePgAccount now createPg…
albertoelopez 166f999
got rid of global.console.log and changed scope of it
albertoelopez 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 hidden or 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,45 @@ | ||
const { Client } = require('pg') | ||
require('dotenv').config() | ||
|
||
const pgModule = {} | ||
let client | ||
|
||
pgModule.startPGDB = ()=>{ | ||
client = new Client({ | ||
host: process.env.HOS, | ||
port: process.env.PORT, | ||
user: process.env.USERNAME, | ||
password: process.env.PASSWORD, | ||
database: process.env.DATABASE | ||
}) | ||
return client.connect() | ||
} | ||
|
||
pgModule.closePGDB = ()=>{ | ||
return client.end() | ||
} | ||
|
||
pgModule.createPgAccount = async (username, password)=>{ | ||
if(!username || !password) return | ||
songz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try{ | ||
await client.query(`CREATE DATABASE IF NOT EXISTS ${username}`) | ||
await client.query(`CREATE USER IF NOT EXISTS ${username} WITH ENCRYPTED password '${password}'`) | ||
await client.query(`GRANT ALL PRIVILEGES ON DATABASE ${username} TO ${username}`) | ||
}catch(err){ | ||
console.log('failed to createPgAccount', err) | ||
songz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new Error(`failed to createPgAccount for user: ${username}`) | ||
} | ||
} | ||
|
||
pgModule.deletePgAccount = async (username)=>{ | ||
if(!username) return | ||
songz marked this conversation as resolved.
Show resolved
Hide resolved
songz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try{ | ||
await client.query(`DROP DATABASE IF EXISTS ${username}`) | ||
await client.query(`DROP USER IF EXISTS ${username}`) | ||
}catch(err){ | ||
console.log('failed to deletePgAccount', err) | ||
throw new Error(`failed to deletePgAccount for database and user: ${username}`) | ||
} | ||
} | ||
|
||
module.exports = pgModule |
This file contains hidden or 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,81 @@ | ||
jest.mock('pg') | ||
const {Client} = require('pg') | ||
const {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg') | ||
|
||
describe('Test PG DB', ()=>{ | ||
beforeEach(()=>{ | ||
jest.clearAllMocks() | ||
}) | ||
let mockClient = { | ||
query: jest.fn().mockReturnValue(Promise.resolve()), | ||
connect: jest.fn().mockReturnValue(Promise.resolve()), | ||
end: jest.fn().mockReturnValue(Promise.resolve()) | ||
} | ||
Client.mockImplementation(function(){ | ||
return mockClient | ||
}) | ||
describe('Test startPGDB && closePGDB', ()=>{ | ||
it('it should call connect when starting PGDB', async ()=>{ | ||
await startPGDB() | ||
expect(mockClient.connect).toHaveBeenCalledTimes(1) | ||
}) | ||
it('it should call end when closing PGDB', async ()=>{ | ||
await closePGDB() | ||
expect(mockClient.end).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
describe('Test create and delete pgAccount', ()=>{ | ||
beforeEach(async ()=>{ | ||
await startPGDB() | ||
}) | ||
afterEach(async ()=>{ | ||
await closePGDB() | ||
}) | ||
describe('Test createPgAccount', ()=>{ | ||
songz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it('it should execute all queries if required arguments are passed into createPgAccount', async ()=>{ | ||
await createPgAccount('username', 'password') | ||
expect(mockClient.query).toHaveBeenCalledTimes(3) | ||
expect(mockClient.query).toHaveBeenNthCalledWith(1, `CREATE DATABASE IF NOT EXISTS username`) | ||
expect(mockClient.query).toHaveBeenNthCalledWith(2, `CREATE USER IF NOT EXISTS username WITH ENCRYPTED password 'password'`) | ||
expect(mockClient.query).toHaveBeenNthCalledWith(3, `GRANT ALL PRIVILEGES ON DATABASE username TO username`) | ||
}) | ||
it('it should not execute any queries in createPgAccount if required arguments are not passed in', async ()=>{ | ||
await createPgAccount() | ||
expect(mockClient.query).toHaveBeenCalledTimes(0) | ||
}) | ||
it('it should check if console.log is called at throw of createPgAccount', async ()=>{ | ||
try{ | ||
console.log = jest.fn() | ||
await mockClient.query.mockReturnValue(Promise.reject()) | ||
const resCreatePgAccount = await createPgAccount('username', 'password') | ||
expect(resCreatePgAccount).rejects.toThrow() | ||
}catch(err){ | ||
expect(console.log).toHaveBeenCalledTimes(1) | ||
} | ||
}) | ||
}) | ||
describe('Test deletePgAccount', ()=>{ | ||
it('it should execute all queries if required arguments are passed into deletePgAccount', async ()=>{ | ||
mockClient.query.mockReturnValue(Promise.resolve()) | ||
await deletePgAccount('username') | ||
expect(mockClient.query).toHaveBeenCalledTimes(2) | ||
expect(mockClient.query).toHaveBeenNthCalledWith(1, `DROP DATABASE IF EXISTS username`) | ||
expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`) | ||
}) | ||
it('it should not execute any queries in deletePgAccount if required arguments are not passed in', async ()=>{ | ||
await deletePgAccount() | ||
expect(mockClient.query).toHaveBeenCalledTimes(0) | ||
}) | ||
it('it should check if console.log is called at throw of deletePgAccount', async ()=>{ | ||
try{ | ||
console.log = jest.fn() | ||
await mockClient.query.mockReturnValue(Promise.reject()) | ||
const resDeletePgAccount = await deletePgAccount('username', 'password') | ||
expect(resDeletePgAccount).rejects.toThrow() | ||
}catch(err){ | ||
expect(console.log).toHaveBeenCalledTimes(1) | ||
} | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains hidden or 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,27 @@ | ||
{ | ||
"name": "databases", | ||
"version": "1.0.0", | ||
"description": "Create databases in postgres, mongoDB and neo4J", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest --coverage" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/garageScript/databases.git" | ||
}, | ||
"author": "garageScript", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/gargeScript/databases/issues" | ||
}, | ||
"homepage": "https://github.com/garageScript/databases#readme", | ||
"devDependencies": { | ||
"dotenv": "^8.2.0", | ||
"jest": "^25.2.4", | ||
"pm2": "^4.2.3" | ||
}, | ||
"dependencies": { | ||
"pg": "^8.0.0" | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.