<commit_before>/*
 This file is a part of libertysoil.org website
 Copyright (C) 2015  Loki Education (Social Enterprise)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/* eslint-env node, mocha */
/* global $dbConfig */
import fs from 'fs';
import { serialize } from 'cookie';
import AWS from 'mock-aws';

import expect from '../../../test-helpers/expect';
import { login } from '../../../test-helpers/api';
import initBookshelf from '../../../src/api/db';
import ApiClient from '../../../src/api/client';
import { API_HOST } from '../../../src/config';
import UserFactory from '../../../test-helpers/factories/user';


let bookshelf = initBookshelf($dbConfig);
let User = bookshelf.model('User');

describe('Client test', () => {
  let client;

  before(async () => {
    client = new ApiClient(API_HOST);
  });

  describe('Base methods', () => {
    it('#GET', async () => {
      let response  = await client.get('/api/v1/test');

      expect(await response.text(), 'to be', 'test message in response');
    });

    it('#HEAD', async () => {
      let response = await client.head('/api/v1/test');

      expect(response.status, 'to be', 200);
    });

    it('#DELETE', async () => {
      let response = await client.del('/api/v1/test');

      expect(await response.text(), 'to be', 'test message in delete response');
    });

    it('#POST', async () => {
      let response = await client.post('/api/v1/test');

      expect(await response.text(), 'to be', 'test message in post response');
    });

    it('#POSTJSON', async () => {
      let response = await client.postJSON('/api/v1/test', {foo: 'bar'});

      expect(await response.text(), 'to be', 'test message in post response');
    });
  });

  it('#checkGeotagExists return false for non existing geotag', async () => {
    let result = await client.checkGeotagExists('nonexistingname');

    expect(result, 'to be false');
  });

  it('#deleteComment works', async () => {
    let result = await client.deleteComment('nonexistingpost', 'nonexistingid');

    expect(result.error, 'to be', 'You are not authorized');
  });

  it('#search works', async () => {
    const result = await client.search('test');

    expect(result, 'to be an', 'object');
    expect(result, 'to have key', 'error');
  });
});

describe('Authenticated client test', () => {
  let user,
    sessionId;

  before(async () => {
    const userAttrs = UserFactory.build();
    user = await User.create(userAttrs.username, userAttrs.password, userAttrs.email);

    user.set('email_check_hash', null);
    await user.save(null, { method: 'update' });
    sessionId = await login(userAttrs.username, userAttrs.password);
  });

  after(async () => {
    await user.destroy();
  });

  it('#uploadImage works', async () => {
    AWS.mock('S3', 'uploadAsync', () => { return { Location: 's3-mocked-location' }; });

    const file = fs.createReadStream('./test-helpers/bulb.png');
    // const result = await client.uploadImage([file]);

    const headers = {
      "cookie": serialize('connect.sid', sessionId)
    };

    const client = new ApiClient(API_HOST, { headers });
    const result = await client.uploadImage([file]);

    expect(result.success, 'to be', true);
  });
});<commit_msg>integration test for "Bad" request form the server<commit_after>/*
 This file is a part of libertysoil.org website
 Copyright (C) 2015  Loki Education (Social Enterprise)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/* eslint-env node, mocha */
/* global $dbConfig,$bookshelf */
import fs from 'fs';
import { serialize } from 'cookie';
import AWS from 'mock-aws';
import sinon from 'sinon';

import expect from '../../../test-helpers/expect';
import { login } from '../../../test-helpers/api';
import initBookshelf from '../../../src/api/db';
import ApiClient from '../../../src/api/client';
import { API_HOST } from '../../../src/config';
import UserFactory from '../../../test-helpers/factories/user';


let bookshelfHelper = initBookshelf($dbConfig);
let User = bookshelfHelper.model('User');

describe('Client test', () => {
  let client;

  before(async () => {
    client = new ApiClient(API_HOST);
  });

  describe('Base methods', () => {
    it('#GET', async () => {
      let response  = await client.get('/api/v1/test');

      expect(await response.text(), 'to be', 'test message in response');
    });

    it('#HEAD', async () => {
      let response = await client.head('/api/v1/test');

      expect(response.status, 'to be', 200);
    });

    it('#DELETE', async () => {
      let response = await client.del('/api/v1/test');

      expect(await response.text(), 'to be', 'test message in delete response');
    });

    it('#POST', async () => {
      let response = await client.post('/api/v1/test');

      expect(await response.text(), 'to be', 'test message in post response');
    });

    it('#POSTJSON', async () => {
      let response = await client.postJSON('/api/v1/test', {foo: 'bar'});

      expect(await response.text(), 'to be', 'test message in post response');
    });
  });

  it('#checkGeotagExists return false for non existing geotag', async () => {
    let result = await client.checkGeotagExists('nonexistingname');

    expect(result, 'to be false');
  });

  it('#deleteComment works', async () => {
    let result = await client.deleteComment('nonexistingpost', 'nonexistingid');

    expect(result.error, 'to be', 'You are not authorized');
  });

  it('#search works', async () => {
    const result = await client.search('test');

    expect(result, 'to be an', 'object');
    expect(result, 'to have key', 'error');
  });

  describe('Exception tests', () => {
    let userModel;

    before(() => {
      // stub bookshelf model to simulate exception
      userModel = $bookshelf.model('User');
      sinon.stub(userModel, 'forge', () => {
        throw new Error('test');
      });
      sinon.stub($bookshelf, 'model', () => {
        return userModel;
      });
    });

    after(() => {
      $bookshelf.model.restore();
      userModel.forge.restore();
    });

    it('should work', async () => {
      return expect(client.getAvailableUsername('test'), 'to be rejected with', 'test');
    });
  });
});

describe('Authenticated client test', () => {
  let user,
    sessionId;

  before(async () => {
    const userAttrs = UserFactory.build();
    user = await User.create(userAttrs.username, userAttrs.password, userAttrs.email);

    user.set('email_check_hash', null);
    await user.save(null, { method: 'update' });
    sessionId = await login(userAttrs.username, userAttrs.password);
  });

  after(async () => {
    await user.destroy();
  });

  it('#uploadImage works', async () => {
    AWS.mock('S3', 'uploadAsync', () => { return { Location: 's3-mocked-location' }; });

    const file = fs.createReadStream('./test-helpers/bulb.png');
    // const result = await client.uploadImage([file]);

    const headers = {
      "cookie": serialize('connect.sid', sessionId)
    };

    const client = new ApiClient(API_HOST, { headers });
    const result = await client.uploadImage([file]);

    expect(result.success, 'to be', true);
  });
});

{'fraction_non_alphanumeric': 0.1671455938697318, 'fraction_numerical': 0.0035919540229885057, 'mean_word_length': 3.2748208802456498, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4799236', 'n_tokens_mistral': 2481, 'n_tokens_neox': 2415, 'n_words': 756}