Skip to content

Commit

Permalink
Merge commit 'ea38bd1c771122eeef373802f221f2f35045598c' into matteo/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
teocomi committed Jul 23, 2020
2 parents eb4f7a9 + ea38bd1 commit 72c4a43
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 75 deletions.
21 changes: 20 additions & 1 deletion modules/auth/tests/apps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,23 @@ describe( 'Apps', ( ) => {
.expect( 401 )
} )
} )
} )

describe( 'GraphQL', ( ) => {

before( async ( ) => {
await knex.migrate.rollback( )
await knex.migrate.latest( )

// TODO: init gql server
} )

after( async ( ) => {

} )

// it( 'should get app info', async ( ) => {
// assert.fail( 'todo' )
// } )

} )
} )
3 changes: 2 additions & 1 deletion modules/core/graph/resolvers/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ module.exports = {
},

async commit( parent, args, context, info ) {
throw new ApolloError( 'not implemented' )
let c = await getCommitById( { id: args.id } )
return c
}

},
Expand Down
1 change: 1 addition & 0 deletions modules/core/graph/resolvers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
},

async user( parent, args, context, info ) {

await validateServerRole( context, 'server:user' )

if ( !args.id )
Expand Down
2 changes: 1 addition & 1 deletion modules/core/services/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module.exports = {
publicOnly = publicOnly !== false //defaults to true if not provided

let query = Acl( )
.columns( [ { id: 'streams.id' }, 'name', 'description', 'isPublic', 'createdAt', 'updatedAt' ] ).select( )
.columns( [ { id: 'streams.id' }, 'name', 'description', 'isPublic', 'createdAt', 'updatedAt', 'role' ] ).select( )
.join( 'streams', 'stream_acl.resourceId', 'streams.id' )
.where( 'stream_acl.userId', userId )

Expand Down
10 changes: 5 additions & 5 deletions modules/core/services/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const ServerAppsScopes = ( ) => knex( 'server_apps_scopes' )
module.exports = {

/*
Tokens
Note: tokens are composed of a 10 char token id and a 32 char token string.
The token string is smoked, salted and hashed and stored in the database.
The token string is smoked, salted and hashed and stored in the database.
*/

Expand Down Expand Up @@ -54,12 +54,12 @@ module.exports = {
return { id: tokenId, token: tokenId + tokenString }
},

// Creates a personal access token for a user with a set of given scopes.
// Creates a personal access token for a user with a set of given scopes.
async createPersonalAccessToken( userId, name, scopes, lifespan ) {

let { id, token } = await module.exports.createToken( { userId, name, scopes, lifespan } )

// Store the relationship
// Store the relationship
await PersonalApiTokens( ).insert( { userId: userId, tokenId: id } )

return token
Expand Down Expand Up @@ -130,4 +130,4 @@ module.exports = {
async getTokenScopes( tokenId ) {

}
}
}
11 changes: 7 additions & 4 deletions modules/core/tests/branches.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,18 @@ describe( 'Branches', ( ) => {

await createBranch( { name: 'master-faster', streamId: stream.id, authorId: user.id } )
await createBranch( { name: 'master-blaster', streamId: stream.id, authorId: user.id } )
await createBranch( { name: 'blaster-farter', streamId: stream.id, authorId: user.id } )

let branches = await getBranchesByStreamId( { streamId: stream.id } )
expect( branches ).to.have.lengthOf( 4 )
let { items, cursor, totalCount } = await getBranchesByStreamId( { streamId: stream.id } )
expect( items ).to.have.lengthOf( 5 )
expect( cursor ).to.exist
expect( totalCount ).to.exist
} )

it( 'Should delete a branch', async ( ) => {
await deleteBranchById( { id: branch.id } )
let branches = await getBranchesByStreamId( { streamId: stream.id } )
expect( branches ).to.have.lengthOf( 3 )
let { items } = await getBranchesByStreamId( { streamId: stream.id } )
expect( items ).to.have.lengthOf( 4 )
} )

} )
81 changes: 39 additions & 42 deletions modules/core/tests/graph.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ describe( 'GraphQL API Core', ( ) => {
expect( res2.body.data.stream.branch.commits.items[ 0 ] ).to.have.property( 'createdAt' )
} )

let commitList

it( 'should retrieve all stream commits', async ( ) => {
let query = `
query {
Expand All @@ -696,6 +698,8 @@ describe( 'GraphQL API Core', ( ) => {
expect( res.body.data.stream.commits.items.length ).to.equal( 10 )
expect( res.body.data.stream.commits.totalCount ).to.equal( 12 )

commitList = res.body.data.stream.commits.items

let query2 = `
query {
stream( id: "${ts1}" ) {
Expand All @@ -714,8 +718,6 @@ describe( 'GraphQL API Core', ( ) => {
`

const res2 = await sendRequest( userA.token, { query: query2 } )
console.log( res2.body.errors )
console.log( res2.body.data.stream.commits )

expect( res2 ).to.be.json
expect( res2.body.errors ).to.not.exist
Expand All @@ -724,50 +726,48 @@ describe( 'GraphQL API Core', ( ) => {
} )

it( 'should retrieve a stream commit', async ( ) => {
const res = await sendRequest( userA.token, { query: `query { stream(id:"${ts1}") { commit(id:"${c2.id}") { id description data } } }` } )
const res = await sendRequest( userA.token, { query: `query { stream( id:"${ts1}" ) { commit( id: "${commitList[ 0 ].id}" ) { id message referencedObject } } }` } )

expect( res ).to.be.json
expect( res.body.errors ).to.not.exist
expect( res.body.data.stream.commit.description ).to.equal( 'test second commit' )
expect( res.body.data.stream.commit.message ).to.equal( 'what a message for commit number 19' ) // should be the last created one

} )
} )

describe( 'Objects', ( ) => {

let myCommit
let myObjs

before( async ( ) => {
let { commit, objs } = createManyObjects( 100, 'noise__' )
let { commit, objs } = generateManyObjects( 100, 'noise__' )
myCommit = commit
myObjs = objs
} )

it( 'should create a commit with its object children', async ( ) => {

const commitRes = await sendRequest( userA.token, { query: `mutation($commit:JSONObject!) { commitCreate(streamId:"${ts1}", commit:$commit) }`, variables: { commit: myCommit } } )

let commitId = commitRes.body.data.commitCreate

expect( commitId ).to.be.a( 'string' )
it( 'should save many objects', async ( ) => {

const objsRes = await sendRequest( userA.token, { query: `mutation($objs:[JSONObject]!) { objectCreate(streamId:"${ts1}", objects: $objs) }`, variables: { objs: myObjs } } )
let everything = [ myCommit, ...myObjs ]
const res = await sendRequest( userA.token, { query: `mutation($objs:[JSONObject]!) { objectCreate(streamId:"${ts1}", objects: $objs) }`, variables: { objs: everything } } )

let objIds = objsRes.body.data.objectCreate
let objIds = res.body.data.objectCreate

expect( objIds.length ).to.equal( 100 )
expect( res ).to.be.json
expect( res.body.errors ).to.not.exist
expect( objIds.length ).to.equal( 101 ) // +1 for the actual "commit" object

} )

it( 'should get a commits objects', async ( ) => {
it( `should get an object's subojects objects`, async ( ) => {
let first = await sendRequest( userA.token, {
query: `
query {
stream(id:"${ts1}") {
stream( id:"${ts1}" ) {
id
name
commit( id:"${myCommit.id}" ) {
object( id:"${myCommit.id}" ) {
createdAt
author{ name }
children( limit: 2 ) {
totalCount
cursor
Expand All @@ -784,19 +784,18 @@ describe( 'GraphQL API Core', ( ) => {
expect( first ).to.be.json
expect( first.body.errors ).to.not.exist
expect( first.body.data.stream ).to.be.an( 'object' )
expect( first.body.data.stream.commit ).to.be.an( 'object' )
expect( first.body.data.stream.commit.children.objects.length ).to.equal( 2 )
expect( first.body.data.stream.object ).to.be.an( 'object' )
expect( first.body.data.stream.object.children.objects.length ).to.equal( 2 )

let second = await sendRequest( userA.token, {
query: `
query {
stream(id:"${ts1}") {
id
name
commit( id:"${myCommit.id}" ) {
object( id:"${myCommit.id}" ) {
createdAt
author{ name }
children( limit: 20, cursor: "${first.body.data.stream.commit.children.cursor}", select: ["sortValueA", "nest.arr[2]"] ) {
children( limit: 20, cursor: "${first.body.data.stream.object.children.cursor}", select: ["sortValueA", "nest.arr[2]"] ) {
totalCount
objects {
id
Expand All @@ -812,22 +811,21 @@ describe( 'GraphQL API Core', ( ) => {
expect( second ).to.be.json
expect( second.body.errors ).to.not.exist
expect( second.body.data.stream ).to.be.an( 'object' )
expect( second.body.data.stream.commit ).to.be.an( 'object' )
expect( second.body.data.stream.commit.children.objects.length ).to.equal( 20 )
expect( second.body.data.stream.commit.children.objects[ 0 ].data.sortValueA ).to.equal( 52 ) // when sorting by id, it's always 52
expect( second.body.data.stream.commit.children.objects[ 0 ].data.nest.arr[ 2 ] ).to.equal( 52 ) // when sorting by id, it's always 52
expect( second.body.data.stream.object ).to.be.an( 'object' )
expect( second.body.data.stream.object.children.objects.length ).to.equal( 20 )
expect( second.body.data.stream.object.children.objects[ 0 ].data.sortValueA ).to.equal( 52 ) // when sorting by id, it's always 52
expect( second.body.data.stream.object.children.objects[ 0 ].data.nest.arr[ 2 ] ).to.equal( 52 ) // when sorting by id, it's always 52
} )

it( 'should query a commits objects', async ( ) => {
it( `should query an object's subojects`, async ( ) => {
let first = await sendRequest( userA.token, {
query: `
query( $query: [JSONObject!], $orderBy: JSONObject ) {
stream(id:"${ts1}") {
id
name
commit( id:"${myCommit.id}" ) {
object( id:"${myCommit.id}" ) {
createdAt
author{ name }
children( limit: 20, select:[ "sortValueA" ], query: $query, orderBy: $orderBy ) {
totalCount
cursor
Expand All @@ -846,10 +844,10 @@ describe( 'GraphQL API Core', ( ) => {
expect( first ).to.be.json
expect( first.body.errors ).to.not.exist
expect( first.body.data.stream ).to.be.an( 'object' )
expect( first.body.data.stream.commit ).to.be.an( 'object' )
expect( first.body.data.stream.commit.children.objects.length ).to.equal( 20 )
expect( first.body.data.stream.commit.children.objects[ 0 ].data.sortValueA ).to.equal( 42 )
expect( first.body.data.stream.commit.children.objects[ 1 ].data.sortValueA ).to.equal( 43 )
expect( first.body.data.stream.object ).to.be.an( 'object' )
expect( first.body.data.stream.object.children.objects.length ).to.equal( 20 )
expect( first.body.data.stream.object.children.objects[ 0 ].data.sortValueA ).to.equal( 42 )
expect( first.body.data.stream.object.children.objects[ 1 ].data.sortValueA ).to.equal( 43 )
} )

} )
Expand Down Expand Up @@ -906,7 +904,7 @@ function sendRequest( auth, obj ) {

// const crypto = require( 'crypto' )

function createManyObjects( shitTon, noise ) {
function generateManyObjects( shitTon, noise ) {
shitTon = shitTon || 10000
noise = noise || Math.random( ) * 100

Expand All @@ -929,19 +927,18 @@ function createManyObjects( shitTon, noise ) {
sortValueB: i * 0.42 * i
}
if ( i % 3 === 0 ) k++
getAFuckingId( baby )
base.__closure[ baby.id ] = 1

if ( i > 1000 )
base.__closure[ baby.id ] = i / 1000
getAnIdForThisOnePlease( baby )

base.__closure[ baby.id ] = 1

objs.push( baby )
}

getAFuckingId( base )
getAnIdForThisOnePlease( base )
return { commit: base, objs: objs }
}

function getAFuckingId( obj ) {
function getAnIdForThisOnePlease( obj ) {
obj.id = obj.id || crypto.createHash( 'md5' ).update( JSON.stringify( obj ) ).digest( 'hex' )
}
17 changes: 10 additions & 7 deletions modules/core/tests/streams.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,21 @@ describe( 'Streams', ( ) => {
} )

it( 'Should get all streams for a user', async ( ) => {
let all = await getUserStreams( { userId: userOne.id } )
expect( all ).to.have.lengthOf( 2 )
let { streams, cursor } = await getUserStreams( { userId: userOne.id } )
// console.log( res )
expect( streams ).to.have.lengthOf( 2 )
expect( cursor ).to.exist
} )

it( 'Should delete a stream', async ( ) => {
const id = await createStream( { name: 'mayfly', description: 'wonderful', ownerId: userOne.id } )
let all = await getUserStreams( { userId: userOne.id } )
expect( all ).to.have.lengthOf( 3 )
expect( all.streams ).to.have.lengthOf( 3 )

await deleteStream( { streamId: id } )

all = await getUserStreams( { userId: userOne.id } )
expect( all ).to.have.lengthOf( 2 )
expect( all.streams ).to.have.lengthOf( 2 )
} )
} )

Expand All @@ -100,7 +102,8 @@ describe( 'Streams', ( ) => {
} )

it( 'Stream should show up in the other users` list', async ( ) => {
let userTwoStreams = await getUserStreams( { userId: userTwo.id } )
let { streams: userTwoStreams } = await getUserStreams( { userId: userTwo.id } )
// console.log( userTwoStreams )
expect( userTwoStreams ).to.have.lengthOf( 1 )
expect( userTwoStreams[ 0 ] ).to.have.property( 'role' )
expect( userTwoStreams[ 0 ].role ).to.equal( 'stream:contributor' )
Expand All @@ -115,7 +118,7 @@ describe( 'Streams', ( ) => {

it( 'Should revoke permissions on stream', async ( ) => {
await revokePermissionsStream( { streamId: testStream.id, userId: userTwo.id } )
let userTwoStreams = await getUserStreams( { userId: userTwo.id } )
let { streams: userTwoStreams } = await getUserStreams( { userId: userTwo.id } )
expect( userTwoStreams ).to.have.lengthOf( 0 )
} )

Expand All @@ -128,4 +131,4 @@ describe( 'Streams', ( ) => {
}
} )
} )
} )
} )
9 changes: 9 additions & 0 deletions package-lock.json

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

Loading

0 comments on commit 72c4a43

Please sign in to comment.