{"size": 3309, "ext": "js", "max_stars_repo_path": "packages/server/modules/core/tests/usersAdmin.spec.js", "max_stars_repo_name": "andreasrudena/speckle-server", "max_stars_repo_stars_event_min_datetime": "2022-03-13T20:25:02.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-13T20:25:02.000Z", "max_issues_repo_path": "packages/server/modules/core/tests/usersAdmin.spec.js", "max_issues_repo_name": "andreasrudena/speckle-server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/server/modules/core/tests/usersAdmin.spec.js", "max_forks_repo_name": "andreasrudena/speckle-server", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 30.6388888889, "max_line_length": 132, "alphanum_fraction": 0.6146872167}
/* istanbul ignore file */
const expect = require( 'chai' ).expect
const assert = require( 'assert' )
const appRoot = require( 'app-root-path' )

const { createUser, getUsers, countUsers, deleteUser, getUserRole, unmakeUserAdmin, makeUserAdmin } = require( '../services/users' )
const { beforeEachContext } = require( `${appRoot}/test/hooks` )

describe( 'User admin @user-services', () => {
  let myTestActor = {
    name: 'Gergo Jedlicska',
    email: 'gergo@jedlicska.com',
    password: 'sn3aky-1337-b1m'
  }

  before( async () => {
    await beforeEachContext( )

    let actorId = await createUser( myTestActor )
    myTestActor.id = actorId
  } )

  it( 'First created user should be admin', async () => {
    let users = await getUsers( 100, 0 )
    expect( users ).to.be.an( 'array' )
    expect( users ).to.have.lengthOf( 1 )
    let firstUser = users[0]

    let userRole = await getUserRole( firstUser.id )
    expect( userRole ).to.equal( 'server:admin' )
  } )

  it( 'Count user knows how to count', async () => {
    expect( await countUsers() ).to.equal( 1 )
    let newUser = { ...myTestActor }
    newUser.name = 'Bill Gates'
    newUser.email = 'bill@gates.com'
    newUser.password = 'testthebest'

    let actorId = await createUser( newUser )

    expect( await countUsers() ).to.equal( 2 )

    await deleteUser( actorId )
    expect( await countUsers() ).to.equal( 1 )
  } )

  it( 'Get users query limit is sanitized to upper limit', async () => {
    let createNewDroid = ( number ) => {
      return {
        name: `${number}`,
        email: `${number}@droidarmy.com`,
        password: 'sn3aky-1337-b1m'
      }
    }

    let userInputs = Array( 250 ).fill().map( ( v, i ) => createNewDroid( i ) )

    expect( await countUsers() ).to.equal( 1 )

    await Promise.all( userInputs.map( userInput => createUser( userInput ) ) )
    expect( await countUsers() ).to.equal( 251 )

    let users = await getUsers( 2000000 )
    expect( users ).to.have.lengthOf( 200 )
  } )

  it( 'Get users offset is applied', async () => {
    let users = await getUsers( 200, 200 )
    expect( users ).to.have.lengthOf( 51 )
  } )

  it( 'User query filters', async () => {
    let users = await getUsers( 100, 0, 'gergo' )
    expect( users ).to.have.lengthOf( 1 )
    let [ user ] = users
    expect( user.email ).to.equal( 'gergo@jedlicska.com' )
  } )

  it( 'Count users applies query', async () => {
    expect( await countUsers( 'droid' ) ).to.equal( 250 )
  } )

  it( 'Change user role modifies role', async () => {
    let [ user ] = await getUsers( 1, 10 )

    let oldRole = await getUserRole( user.id )
    expect( oldRole ).to.equal( 'server:user' )

    await makeUserAdmin( { userId: user.id } )
    let newRole = await getUserRole( user.id )
    expect( newRole ).to.equal( 'server:admin' )

    await unmakeUserAdmin( { userId: user.id } )
    newRole = await getUserRole( user.id )
    expect( newRole ).to.equal( 'server:user' )
  } )

  it( 'Ensure at least one admin remains in the server', async () => {
    try {
      await unmakeUserAdmin( { userId: myTestActor.id, role: 'server:admin' } )
      assert.fail( 'This should have failed' )
    } catch ( err ) {
      expect( err.message ).to.equal( 'Cannot remove the last admin role from the server' )
    }
  } )
} )
