<commit_before>/* global describe it beforeEach */

import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import { assert } from 'meteor/practicalmeteor:chai';

import { Posts } from './posts.js';

if (Meteor.isServer) {
  describe('Posts', () => {
    describe('methods', () => {
      const userId = Random.id();
      const otherUserId = Random.id();
      let post;
      beforeEach(() => {
        Meteor.users.remove({}); 
        Posts.remove({});
        post = Posts.insert({
          title: 'test title',
          content: 'test content',
          createdAt: new Date(),
          isPublic: false,
          ownerId: userId,
          ownerName: 'jdoe',
        });
      });

      it('can delete owned post', () => {
        const deletePost = Meteor.server.method_handlers['posts.remove'];
        const invocation = { userId };
        deletePost.apply(invocation, [post]);
        assert.equal(Posts.find().count(), 0);
      });

      it('cannot delete other\'s post', () => {
        const deletePost = Meteor.server.method_handlers['posts.remove'];
        const invocation = { otherUserId };
        assert.throws(
          () => {
            deletePost.apply(invocation, [post]);
          },
          Meteor.Error,
          'not-authorized'
        );
      });

      it('can make public owned post', () => {
        const makePublic = Meteor.server.method_handlers['posts.setIsPublic'];
        const invocation = { userId };
        makePublic.apply(invocation, [post, true]);
        assert.equal(Posts.findOne(post).isPublic, true);
      });

      it('cannot make public other\'s post', () => {
        const makePublic = Meteor.server.method_handlers['posts.setIsPublic'];
        const invocation = { otherUserId };
        assert.throws(
          () => {
            makePublic.apply(invocation, [post, true]);
          },
          Meteor.Error,
          'not-authorized'
        );
      });

      it('can insert a new post when logged in', () => {
        const insertPost = Meteor.server.method_handlers['posts.insert'];
        const invocation = {userId: Accounts.createUser({ username: 'jdoe', password: '<PASSWORD>'})};
        Posts.remove({});
        insertPost.apply(invocation, ['test title', 'test content']);
        assert.equal(Posts.findOne().ownerName, 'jdoe');
      });<commit_msg>linting defects repaired<commit_after>/* global describe it beforeEach */

import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
import { Accounts } from 'meteor/accounts-base';
import { assert } from 'meteor/practicalmeteor:chai';

import { Posts } from './posts.js';

if (Meteor.isServer) {
  describe('Posts', () => {
    describe('methods', () => {
      const userId = Random.id();
      const otherUserId = Random.id();
      let post;
      beforeEach(() => {
        Meteor.users.remove({});
        Posts.remove({});
        post = Posts.insert({
          title: 'test title',
          content: 'test content',
          createdAt: new Date(),
          isPublic: false,
          ownerId: userId,
          ownerName: 'jdoe',
        });
      });

      it('can delete owned post', () => {
        const deletePost = Meteor.server.method_handlers['posts.remove'];
        const invocation = { userId };
        deletePost.apply(invocation, [post]);
        assert.equal(Posts.find().count(), 0);
      });

      it('cannot delete other\'s post', () => {
        const deletePost = Meteor.server.method_handlers['posts.remove'];
        const invocation = { otherUserId };
        assert.throws(
          () => {
            deletePost.apply(invocation, [post]);
          },
          Meteor.Error,
          'not-authorized'
        );
      });

      it('can make public owned post', () => {
        const makePublic = Meteor.server.method_handlers['posts.setIsPublic'];
        const invocation = { userId };
        makePublic.apply(invocation, [post, true]);
        assert.equal(Posts.findOne(post).isPublic, true);
      });

      it('cannot make public other\'s post', () => {
        const makePublic = Meteor.server.method_handlers['posts.setIsPublic'];
        const invocation = { otherUserId };
        assert.throws(
          () => {
            makePublic.apply(invocation, [post, true]);
          },
          Meteor.Error,
          'not-authorized'
        );
      });

      it('can insert a new post when logged in', () => {
        const insertPost = Meteor.server.method_handlers['posts.insert'];
        const invocation = { userId: Accounts.createUser({ username: 'jdoe', password: '<PASSWORD>' }) };
        Posts.remove({});
        insertPost.apply(invocation, ['test title', 'test content']);
        assert.equal(Posts.findOne().ownerName, 'jdoe');
      });
{'fraction_non_alphanumeric': 0.15591174021648627, 'fraction_numerical': 0.00041631973355537054, 'mean_word_length': 2.2400539447066756, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '3713108', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1349, 'n_words': 290}