<commit_before>/* eslint-env mocha */
/* eslint no-unused-expressions:0 */

import sinon from 'sinon'
import request from 'supertest'
import { ChannelModelAPI } from '../../src/model/channels'
import * as server from '../../src/server'
import * as testUtils from '../testUtils'

const {auth} = testUtils

xdescribe('API Integration Tests', () =>

  xdescribe('Restart REST Api testing', () => {
    let authDetails = {}

    const channel = new ChannelModelAPI({
      name: 'TestChannel1',
      urlPattern: 'test/sample',
      allow: ['PoC', 'Test1', 'Test2'],
      routes: [{
        name: 'test route',
        host: 'localhost',
        port: 9876,
        primary: true
      }],
      txViewAcl: ['group1'],
      txViewFullAcl: []
    })

    before(done =>
      auth.setupTestUsers(err => {
        if (err) { return done(err) }
        channel.save(err => {
          if (err) { return done(err) }
          server.start({apiPort: 8080}, () => done())
        })
      })
    )

    after(done =>
      auth.cleanupTestUsers(err => {
        if (err) { return done(err) }
        ChannelModelAPI.remove(err => {
          if (err) { return done(err) }
          server.stop(() => done())
        })
      })
    )

    beforeEach(() => { authDetails = auth.getAuthDetails() })

    xdescribe('*restart()', () => {
      it('should successfully send API request to restart the server', (done) => {
        const stub = sinon.stub(server, 'startRestartServerTimeout')
        request('https://localhost:8080')
          .post('/restart')
          .set('auth-username', testUtils.rootUser.email)
          .set('auth-ts', authDetails.authTS)
          .set('auth-salt', authDetails.authSalt)
          .set('auth-token', authDetails.authToken)
          .send()
          .expect(200)
          .end((err, res) => {
            if (err) {
              return done(err)
            } else {
              stub.calledOnce.should.be.true
              return done()
            }
          })
      })

      it('should not allow non admin user to restart the server', done =>
        request('https://localhost:8080')
          .post('/restart')
          .set('auth-username', testUtils.nonRootUser.email)
          .set('auth-ts', authDetails.authTS)
          .set('auth-salt', authDetails.authSalt)
          .set('auth-token', authDetails.authToken)
          .send()
          .expect(403)
          .end((err, res) => {
            if (err) {
              return done(err)
            } else {
              return done()
            }
          })
      )
    })
  })
)<commit_msg>Refactor restartAPI tests<commit_after>/* eslint-env mocha */
/* eslint no-unused-expressions:0 */

import sinon from 'sinon'
import request from 'supertest'
import { ChannelModelAPI } from '../../src/model/channels'
import { MediatorModelAPI } from '../../src/model/mediators'
import * as constants from '../constants'
import * as server from '../../src/server'
import * as testUtils from '../utils'
import { promisify } from 'util'

describe('API Integration Tests', () =>

  describe('Restart REST Api testing', () => {
    let authDetails = {}
    const { SERVER_PORTS } = constants

    const channel = new ChannelModelAPI({
      name: 'TestChannel1',
      urlPattern: 'test/sample',
      allow: ['PoC', 'Test1', 'Test2'],
      routes: [{
        name: 'test route',
        host: 'localhost',
        port: 9876,
        primary: true
      }],
      txViewAcl: ['group1'],
      txViewFullAcl: []
    })

    before(async () => {
      await testUtils.setupTestUsers()
      await channel.save()
      await promisify(server.start)({ apiPort: SERVER_PORTS.apiPort })
    })

    after(async () => {
      await testUtils.cleanupTestUsers()
      await ChannelModelAPI.remove()
      await promisify(server.stop)()
    })

    beforeEach(() => { authDetails = testUtils.getAuthDetails() })

    describe('*restart()', () => {
      it('should successfully send API request to restart the server', async () => {
        const stub = sinon.stub(server, 'startRestartServerTimeout')
        await request(constants.BASE_URL)
          .post('/restart')
          .set('auth-username', testUtils.rootUser.email)
          .set('auth-ts', authDetails.authTS)
          .set('auth-salt', authDetails.authSalt)
          .set('auth-token', authDetails.authToken)
          .send()
          .expect(200)
        stub.calledOnce.should.be.true()
      })

      it('should not allow non admin user to restart the server', async () => {
        await request(constants.BASE_URL)
          .post('/restart')
          .set('auth-username', testUtils.nonRootUser.email)
          .set('auth-ts', authDetails.authTS)
          .set('auth-salt', authDetails.authSalt)
          .set('auth-token', authDetails.authToken)
          .send()
          .expect(403)
      })
    })
  })
)
{'fraction_non_alphanumeric': 0.15450819672131147, 'fraction_numerical': 0.00860655737704918, 'mean_word_length': 2.0853350189633377, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2119969', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1445, 'n_words': 314}