{"size": 3013, "ext": "js", "max_stars_repo_path": "src/www/api/administrator/subscriptions/set-charge-flagged.test.js", "max_stars_repo_name": "userdashboard/stripe-subscriptions", "max_stars_repo_stars_event_min_datetime": "2019-08-20T23:29:29.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-03T15:09:49.000Z", "max_issues_repo_path": "src/www/api/administrator/subscriptions/set-charge-flagged.test.js", "max_issues_repo_name": "userappstore/stripe-subscriptions", "max_issues_repo_issues_event_min_datetime": "2018-07-19T16:18:58.000Z", "max_issues_repo_issues_event_max_datetime": "2019-07-07T14:45:26.000Z", "max_forks_repo_path": "src/www/api/administrator/subscriptions/set-charge-flagged.test.js", "max_forks_repo_name": "userappstore/stripe-subscriptions", "max_forks_repo_forks_event_min_datetime": "2019-08-04T12:00:55.000Z", "max_forks_repo_forks_event_max_datetime": "2021-06-05T06:15:45.000Z"}
{"max_stars_count": 4, "max_issues_count": 7, "max_forks_count": 4, "avg_line_length": 40.1733333333, "max_line_length": 126, "alphanum_fraction": 0.6621307667}
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')

describe('/api/administrator/subscriptions/set-charge-flagged', function () {
  afterEach(TestHelper.deleteOldWebhooks)
  beforeEach(TestHelper.setupWebhook)
  describe('exceptions', () => {
    describe('invalid-chargeid', () => {
      it('missing querystring charge', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/set-charge-flagged')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-chargeid')
      })

      it('invalid querystring charge', async () => {
        const administrator = await TestHelper.createOwner()
        const req = TestHelper.createRequest('/api/administrator/subscriptions/set-charge-flagged?chargeid=invalid')
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-chargeid')
      })
    })

    describe('invalid-charge', () => {
      it('ineligible querystring charge is already flagged', async () => {
        const administrator = await TestStripeAccounts.createOwnerWithPlan()
        const user = await TestStripeAccounts.createUserWithPaymentMethod()
        await TestHelper.createSubscription(user, administrator.plan.id)
        await TestHelper.flagCharge(administrator, user.charge.id)
        const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-charge-flagged?chargeid=${user.charge.id}`)
        req.account = administrator.account
        req.session = administrator.session
        let errorMessage
        try {
          await req.patch(req)
        } catch (error) {
          errorMessage = error.message
        }
        assert.strictEqual(errorMessage, 'invalid-charge')
      })
    })
  })

  describe('returns', () => {
    it('object', async () => {
      const administrator = await TestStripeAccounts.createOwnerWithPlan()
      const user = await TestStripeAccounts.createUserWithPaymentMethod()
      await TestHelper.createSubscription(user, administrator.plan.id)
      const req = TestHelper.createRequest(`/api/administrator/subscriptions/set-charge-flagged?chargeid=${user.charge.id}`)
      req.account = administrator.account
      req.session = administrator.session
      req.filename = __filename
      req.saveResponse = true
      const charge = await req.patch()
      assert.strictEqual(charge.fraud_details.user_report, 'fraudulent')
    })
  })
})
