<commit_before>_ = require 'underscore'
sinon = require 'sinon'
Backbone = require 'backbone'
rewire = require 'rewire'
routes = rewire '../routes'
fs = require 'fs'
{ EventEmitter } = require 'events'
{ resolve }  = require 'path'
{ fabricate } = require 'antigravity'

describe 'About2 routes', ->

  beforeEach ->
    sinon.stub Backbone, 'sync'
    @req = { params: { id: 'foo' } }
    @res = {
      render: sinon.stub()
      redirect: sinon.stub()
      locals: { sd: { API_URL: 'http://localhost:5000', CURRENT_PATH: '/post/post-id' } }
      status: sinon.stub()
      send: sinon.stub()
    }
    routes.__set__ 'client', @client = { getFile: sinon.stub(), putBuffer: sinon.stub() }
    routes.__set__ 'request', @request = {}

  afterEach ->
    Backbone.sync.restore()

  describe '#index', ->

    it 'reads the json into locals', (done) ->
      @res.render = (tmpl, locals) =>
        locals.foo.should.equal "bar"
        done()
      @request.get = -> end: (cb) -> cb null, { text: '{"foo": "bar"}' }
      routes.index @req, @res

  describe '#adminOnly', ->

    it 'restricts admins', ->
      @req.user = new Backbone.Model(type: 'User')
      routes.adminOnly @req, @res, next = sinon.stub()
      next.args[0][0].toString().should.containEql "You must be logged in as an admin"

  describe '#upload', ->

    it 'uploads the file to s3', ->
      @req.body = { foo: 'bar' }
      routes.upload @req, @res
      @client.putBuffer.args[0][0].toString().should.equal JSON.stringify @req.body
      @client.putBuffer.args[0][3](null, {})
      @res.send.args[0][0].should.equal 200<commit_msg>tests<commit_after>_ = require 'underscore'
sinon = require 'sinon'
Backbone = require 'backbone'
rewire = require 'rewire'
routes = rewire '../routes'
fs = require 'fs'
{ EventEmitter } = require 'events'
{ resolve }  = require 'path'
{ fabricate } = require 'antigravity'

describe 'About2 routes', ->

  beforeEach ->
    sinon.stub Backbone, 'sync'
    @req = { params: { id: 'foo' } }
    @res = {
      render: sinon.stub()
      redirect: sinon.stub()
      locals: { sd: { API_URL: 'http://localhost:5000', CURRENT_PATH: '/post/post-id' } }
      status: sinon.stub()
      send: sinon.stub()
    }
    routes.__set__ 'client', @client = { getFile: sinon.stub(), putBuffer: sinon.stub() }
    routes.__set__ 'request', @request = {}

  afterEach ->
    Backbone.sync.restore()

  describe '#index', ->

    it 'reads the json into locals', (done) ->
      @res.render = (tmpl, locals) =>
        locals.foo.should.equal "bar"
        done()
      @request.get = -> end: (cb) -> cb null, { text: '{"foo": "bar"}' }
      routes.index @req, @res

  describe '#adminOnly', ->

    it 'restricts admins', ->
      @req.user = new Backbone.Model(type: 'User')
      routes.adminOnly @req, @res, next = sinon.stub()
      next.args[0][0].toString().should.containEql "You must be logged in as an admin"

  describe '#upload', ->

    it 'uploads the file to s3', ->
      @req.body = { foo: 'bar' }
      routes.upload @req, @res
      @client.putBuffer.args[0][0].toString().should.equal JSON.stringify @req.body
      @client.putBuffer.args[0][3](null, {})
      @res.send.args[0][0].should.equal 200

  describe '#sendSMS', ->

    twilioConstructorArgs = null; twilioSendSmsArgs = null; send = null; next = null

    beforeEach ->
      twilio = routes.__get__ 'twilio'
      twilio.RestClient = class TwilioClientStub
        constructor: -> twilioConstructorArgs = arguments
        sendSms: -> twilioSendSmsArgs = arguments
      routes.sendSMS { param: -> '555 111 2222' }, { send: send = sinon.stub() }, next = sinon.stub()

    it 'sends a link with a valid phone number', ->
      twilioSendSmsArgs[0].to.should.equal '555 111 2222'
      twilioSendSmsArgs[0].body.should.containEql "Download the new Artsy iPhone app here: "
      twilioSendSmsArgs[1] null, 'SUCCESS!'
      send.args[0][0].msg.should.containEql 'success'

    it 'throws an error if twilio doesnt like it', ->
      twilioSendSmsArgs[1] 'fail', { message: 'You suck!' }
      next.args[0][0].should.containEql 'fail'
{'fraction_non_alphanumeric': 0.1661800486618005, 'fraction_numerical': 0.01508515815085158, 'mean_word_length': 2.9003795066413662, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '901510', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1417, 'n_words': 342}