<commit_before>import app from '../server/index';
import { expect } from 'chai';
import request from 'supertest-as-promised';
import * as utils from '../test/utils';
import constants from '../server/constants/activities';
import models from '../server/models';
import roles from '../server/constants/roles';
import _ from 'lodash';
import Promise from 'bluebird';

const application = utils.data('application');
const userData = utils.data('user1');
const user2Data = utils.data('user2');
const collectiveData = utils.data('collective1');
const collective2Data = utils.data('collective2');
const collective3Data = utils.data('collective3');
const notificationData = { type: constants.COLLECTIVE_TRANSACTION_CREATED };

const {
  User,
  Collective,
  Notification,
  Tier,
  Order
} = models;

describe("notification.model.test.js", () => {

  let user;
  let user2;
  let collective;
  let collective2;

  beforeEach(() => utils.resetTestDB());

  beforeEach(() => {
    const promises = [User.createUserWithCollective(userData), User.createUserWithCollective(user2Data), Collective.create(collectiveData), Collective.create(collective2Data)];
    return Promise.all(promises).then((results) => {
      user = results[0];
      user2 = results[1];
      collective = results[2];
      collective2 = results[3];
      return collective.addUserWithRole(user, 'HOST')
    })
  });


  it('subscribes to the notifications for the `collective.transaction.approved` email', () =>
    request(app)
      .post(`/groups/${collective.id}/activities/collective.transaction.approved/subscribe`)
      .set('Authorization', `Bearer ${user.jwt()}`)
      .send({ api_key: application.api_key })
      .expect(200)
      .then(res => {
        expect(res.body.active).to.be.true;

        return Notification.findAndCountAll({
          where: {
            UserId: user.id,
            CollectiveId: collective.id,
            type: 'collective.transaction.approved'
          }
        });
      })
      .tap(res => expect(res.count).to.equal(1)));

  it(`disables notification for the ${notificationData.type} email`, () =>
    request(app)
      .post(`/groups/${collective.id}/activities/${notificationData.type}/unsubscribe`)
      .set('Authorization', `Bearer ${user.jwt()}`)
      .send({ api_key: application.api_key })
      .expect(200)
      .then(() =>
        Notification.findAndCountAll({where: {
          UserId: user.id,
          CollectiveId: collective.id,
          type: notificationData.type
        }}))
      .tap(res => expect(res.count).to.equal(0)));

  it('fails to add another notification if one exists', () =>
    request(app)
      .post(`/groups/${collective.id}/activities/${notificationData.type}/subscribe`)
      .set('Authorization', `Bearer ${user.jwt()}`)
      .send({ api_key: application.api_key })
      .expect(400)
      .then(res => {
        expect(res.body.error.message).to.equal('Already subscribed to this type of activity');
        return Notification.findAndCountAll({
          where: {
            UserId: user.id,
            CollectiveId: collective.id,
            type: notificationData.type
          }
        });
      })
      .tap(res => expect(res.count).to.equal(1)));

  it('fails to add a notification if not a member of the collective', () =>
    request(app)
      .post(`/groups/${collective2.id}/activities/collective.transaction.approved/subscribe`)
      .set('Authorization', `Bearer ${user.jwt()}`)
      .send({ api_key: application.api_key })
      .expect(403)
      .then(() => Notification.findAndCountAll({where: {
          UserId: user.id,
          CollectiveId: collective2.id,
          type: notificationData.type
        }}))
      .tap(res => expect(res.count).to.equal(0)));

  it('automatically subscribe new members to `collective.transaction.created`, `collective.expense.created` and `collective.monthlyreport` events', () =>
    request(app)
      .post('/groups')
      .send({
        api_key: application.api_key,
        collective: Object.assign(collective3Data, { users: [{ email: user2.email, role: roles.HOST},{ email: utils.data("user3").email, role: roles.ADMIN}]})
      })
      .expect(200)
      .then(res => Notification.findAndCountAll({where: {
          CollectiveId: res.body.id
        }}))
      .tap(res => {
        const notifications = res.rows;
        const types = _.map(notifications, 'type').sort();
        expect(types).to.deep.equal([
          'collective.expense.created',
          'collective.expense.created',
          'collective.monthlyreport',
          'collective.transaction.created',
          'mailinglist.admins',
          'mailinglist.host'
        ]);
      })
      .tap(res => expect(res.count).to.equal(6)));

  describe('getSubscribers', () => {<commit_msg>fix for notification.model.test<commit_after>import app from '../server/index';
import { expect } from 'chai';
import request from 'supertest-as-promised';
import * as utils from '../test/utils';
import constants from '../server/constants/activities';
import models from '../server/models';
import roles from '../server/constants/roles';
import _ from 'lodash';
import Promise from 'bluebird';

const application = utils.data('application');
const userData = utils.data('user1');
const user2Data = utils.data('user2');
const collectiveData = utils.data('collective1');
const collective2Data = utils.data('collective2');
const collective3Data = utils.data('collective3');
const notificationData = { type: constants.COLLECTIVE_TRANSACTION_CREATED };

const {
  User,
  Collective,
  Notification,
  Tier,
  Order
} = models;

describe("notification.model.test.js", () => {

  let user;
  let user2;
  let collective;
  let collective2;

  beforeEach(() => utils.resetTestDB());

  beforeEach(() => {
    const promises = [User.createUserWithCollective(userData), User.createUserWithCollective(user2Data), Collective.create(collectiveData), Collective.create(collective2Data)];
    return Promise.all(promises).then((results) => {
      user = results[0];
      user2 = results[1];
      collective = results[2];
      collective2 = results[3];
      return collective.addUserWithRole(user, 'HOST')
    })
  });


  it('subscribes to the notifications for the `collective.transaction.approved` email', () =>
    request(app)
      .post(`/groups/${collective.id}/activities/collective.transaction.approved/subscribe`)
      .set('Authorization', `Bearer ${user.jwt()}`)
      .send({ api_key: application.api_key })
      .expect(200)
      .then(res => {
        expect(res.body.active).to.be.true;

        return Notification.findAndCountAll({
          where: {
            UserId: user.id,
            CollectiveId: collective.id,
            type: 'collective.transaction.approved'
          }
        });
      })
      .tap(res => expect(res.count).to.equal(1)));

  it(`disables notification for the ${notificationData.type} email`, () =>
    request(app)
      .post(`/groups/${collective.id}/activities/${notificationData.type}/unsubscribe`)
      .set('Authorization', `Bearer ${user.jwt()}`)
      .send({ api_key: application.api_key })
      .expect(200)
      .then(() =>
        Notification.findAndCountAll({where: {
          UserId: user.id,
          CollectiveId: collective.id,
          type: notificationData.type
        }}))
      .tap(res => expect(res.count).to.equal(0)));

  it('fails to add another notification if one exists', () =>
    request(app)
      .post(`/groups/${collective.id}/activities/${notificationData.type}/subscribe`)
      .set('Authorization', `Bearer ${user.jwt()}`)
      .send({ api_key: application.api_key })
      .expect(400)
      .then(res => {
        expect(res.body.error.message).to.equal('Already subscribed to this type of activity');
        return Notification.findAndCountAll({
          where: {
            UserId: user.id,
            CollectiveId: collective.id,
            type: notificationData.type
          }
        });
      })
      .tap(res => expect(res.count).to.equal(1)));

  it('fails to add a notification if not a member of the collective', () =>
    request(app)
      .post(`/groups/${collective2.id}/activities/collective.transaction.approved/subscribe`)
      .set('Authorization', `Bearer ${user.jwt()}`)
      .send({ api_key: application.api_key })
      .expect(403)
      .then(() => Notification.findAndCountAll({where: {
          UserId: user.id,
          CollectiveId: collective2.id,
          type: notificationData.type
        }}))
      .tap(res => expect(res.count).to.equal(0)));

  it('automatically subscribe new members to `collective.transaction.created`, `collective.expense.created` and `collective.monthlyreport` events', () =>
    request(app)
      .post('/groups')
      .send({
        api_key: application.api_key,
        group: Object.assign(collective3Data, { users: [
          { email: user2.email, role: roles.HOST },
          { email: utils.data("user3").email, role: roles.ADMIN }
        ]})
      })
      .expect(200)
      .then(res => Notification.findAndCountAll({where: {
          CollectiveId: res.body.id
        }}))
      .tap(res => {
        const notifications = res.rows;
        const types = _.map(notifications, 'type').sort();
        expect(types).to.deep.equal([
          'collective.expense.created',
          'collective.expense.created',
          'collective.monthlyreport',
          'collective.transaction.created',
          'mailinglist.admins',
          'mailinglist.host'
        ]);
      })
      .tap(res => expect(res.count).to.equal(6)));

  describe('getSubscribers', () => {
{'fraction_non_alphanumeric': 0.15585492227979275, 'fraction_numerical': 0.008911917098445596, 'mean_word_length': 2.891532258064516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, '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': '2340367', 'n_tokens_mistral': 2920, 'n_tokens_neox': 2864, 'n_words': 536}