<commit_before>config     = require '../config'
fixtures   = require '../test/fixtures'
superagent = require 'superagent'
request    = require 'request'
should     = require 'should'
Objects    = require '../lib/argumenta/objects'
{Argument} = Objects

base = 'http://localhost:3000'

class HttpHelper

  constructor: (@agent, @basepath) ->

  absUrl: (path) ->
    if ~path.search /https?:\/\// then path else @basepath + path

  get: (path, cb) ->
    @agent.get( @absUrl path ).end( cb )

  post: (path, data, cb) ->
    @agent.post( @absUrl path ).send( data ).end( cb )

  put: (path, data, cb) ->
    @agent.put( @absUrl path ).send( data ).end( cb )

  del: (path, cb) ->
    @agent.del( @absUrl path ).end( cb )

# Http helper - provides http methods with agent & base url.
httpHelper = (callback) ->
  agent = superagent.agent()
  callback new HttpHelper( agent, base )

# Temp agent helper - provides callback with get, post, put, delete.
tempAgent = (callback) ->
  httpHelper (http) ->
    post = http.post.bind http
    get  = http.get.bind http
    put  = http.put.bind http
    del  = http.del.bind http
    callback get, post, put, del

# Session helper - provides logged in user with new account, get, post.
session = (callback) ->
  tempAgent (get, post, put, del) ->
    user = fixtures.uniqueUserData()
    post '/users', user, (err, res) ->
      should.not.exist err
      res.status.should.equal 200
      res.text.should.match new RegExp 'Logged in as <a href="/'+user.username+'">'+user.username+'</a>'
      callback user, get, post, put, del

# Logged out session helper - provides a user session just after logout, get, post.
loggedOutSession = (callback) ->
  session (user, get, post, put, del) ->
    get '/logout', (err, res) ->
      should.not.exist err
      res.status.should.equal 200
      callback user, get, post, put, del

# No session helper - provides user without account, get, post.
noSession = (callback) ->
  user = fixtures.uniqueUser()
  tempAgent (get, post, put, del) ->
    callback user, get, post, put, del

# Assertion helper - checks if text matches argument
matchesArgument = (text, arg) ->
  for s in arg.premises.concat [arg.title, arg.conclusion]
    text.should.match new RegExp s

# Session with argument helper - provides user, argument data, get, post
sessionWithArgument = (callback) ->
  session (user, get, post, put, del) ->
    data = fixtures.uniqueArgumentData()
    post '/arguments', data, (err, res) ->
      should.not.exist err
      res.status.should.equal 200
      callback user, data, get, post, put, del

# Session with tag helper - provides user, argument, tag, get, post
sessionWithTag = (callback) ->
  sessionWithArgument (user, argumentData, get, post, put, del) ->
    argument = new Argument argumentData
    tag = fixtures.validSupportTag()
    tag.targetType = 'proposition'
    tag.targetSha1 = argument.propositions[0].sha1()
    tag.sourceType = 'proposition'
    tag.sourceSha1 = argument.propositions[1].sha1()
    tagData = tag.data()
    post '/tags.json', tagData, (res) ->
      res.status.should.equal 201
      callback user, argumentData, tagData, get, post, put, del

# Verify JSONP Helper - Given a JSONP response, invokes callback with json.
verifyJSONP = (res, callback) ->
  res.type.should.equal 'text/javascript'
  res.text.should.exist
  jsonp = res.text
  jsonpCallback = (json) ->
    callback(json)
  eval jsonp

# Http helpers.
http = new HttpHelper( superagent.agent(), base )
get = http.get.bind http
post = http.post.bind http

# Request helpers.
req_get = (path, cb) ->
  request abs_url(path), cb

req_post = (path, data, cb) ->
  options =
    uri: abs_url(path)
    form: data
  request.post options, cb

describeTests = () ->
  app = require '../app'
  describeAppTests config.storageType, app

# Describes app tests for the given storage type and app.
describeAppTests = (type, app) ->

  describe "App with #{type} store", ->

    before (done) ->
      unless app.config.appMode is 'testing'
        return console.log "Won't drop non-testing database"

      app.argumenta.storage.clearAll null, (err) ->
        should.not.exist err
        done()

    describe '/', () ->
      describe 'GET /', ->
        it 'should respond with index and links to log in', (done) ->
          get '/', (res) ->
            res.status.should.equal 200
            res.text.should.match /Argumenta/
            res.text.should.match /Sign in.*or.*Join now!/
            done()

    describe '/users', () ->

      describe 'POST /users', () ->
        it 'should create a new user and sign in', (done) ->
          user =
            username: 'tester'
            password: '<PASSWORD>'
            email:    '<EMAIL>'
          post '/users', user, (err, res) ->
            res.status.should.equal 200
            res.text.should.match /tester/
            res.text.should.match new RegExp 'Logged in as <a href="/tester">tester</a>'
            done()

        it 'should refuse to create an invalid user', (done) -><commit_msg>allow testing in any mode<commit_after>config     = require '../config'
fixtures   = require '../test/fixtures'
superagent = require 'superagent'
request    = require 'request'
should     = require 'should'
Objects    = require '../lib/argumenta/objects'
{Argument} = Objects

base = 'http://localhost:3000'

class HttpHelper

  constructor: (@agent, @basepath) ->

  absUrl: (path) ->
    if ~path.search /https?:\/\// then path else @basepath + path

  get: (path, cb) ->
    @agent.get( @absUrl path ).end( cb )

  post: (path, data, cb) ->
    @agent.post( @absUrl path ).send( data ).end( cb )

  put: (path, data, cb) ->
    @agent.put( @absUrl path ).send( data ).end( cb )

  del: (path, cb) ->
    @agent.del( @absUrl path ).end( cb )

# Http helper - provides http methods with agent & base url.
httpHelper = (callback) ->
  agent = superagent.agent()
  callback new HttpHelper( agent, base )

# Temp agent helper - provides callback with get, post, put, delete.
tempAgent = (callback) ->
  httpHelper (http) ->
    post = http.post.bind http
    get  = http.get.bind http
    put  = http.put.bind http
    del  = http.del.bind http
    callback get, post, put, del

# Session helper - provides logged in user with new account, get, post.
session = (callback) ->
  tempAgent (get, post, put, del) ->
    user = fixtures.uniqueUserData()
    post '/users', user, (err, res) ->
      should.not.exist err
      res.status.should.equal 200
      res.text.should.match new RegExp 'Logged in as <a href="/'+user.username+'">'+user.username+'</a>'
      callback user, get, post, put, del

# Logged out session helper - provides a user session just after logout, get, post.
loggedOutSession = (callback) ->
  session (user, get, post, put, del) ->
    get '/logout', (err, res) ->
      should.not.exist err
      res.status.should.equal 200
      callback user, get, post, put, del

# No session helper - provides user without account, get, post.
noSession = (callback) ->
  user = fixtures.uniqueUser()
  tempAgent (get, post, put, del) ->
    callback user, get, post, put, del

# Assertion helper - checks if text matches argument
matchesArgument = (text, arg) ->
  for s in arg.premises.concat [arg.title, arg.conclusion]
    text.should.match new RegExp s

# Session with argument helper - provides user, argument data, get, post
sessionWithArgument = (callback) ->
  session (user, get, post, put, del) ->
    data = fixtures.uniqueArgumentData()
    post '/arguments', data, (err, res) ->
      should.not.exist err
      res.status.should.equal 200
      callback user, data, get, post, put, del

# Session with tag helper - provides user, argument, tag, get, post
sessionWithTag = (callback) ->
  sessionWithArgument (user, argumentData, get, post, put, del) ->
    argument = new Argument argumentData
    tag = fixtures.validSupportTag()
    tag.targetType = 'proposition'
    tag.targetSha1 = argument.propositions[0].sha1()
    tag.sourceType = 'proposition'
    tag.sourceSha1 = argument.propositions[1].sha1()
    tagData = tag.data()
    post '/tags.json', tagData, (res) ->
      res.status.should.equal 201
      callback user, argumentData, tagData, get, post, put, del

# Verify JSONP Helper - Given a JSONP response, invokes callback with json.
verifyJSONP = (res, callback) ->
  res.type.should.equal 'text/javascript'
  res.text.should.exist
  jsonp = res.text
  jsonpCallback = (json) ->
    callback(json)
  eval jsonp

# Http helpers.
http = new HttpHelper( superagent.agent(), base )
get = http.get.bind http
post = http.post.bind http

# Request helpers.
req_get = (path, cb) ->
  request abs_url(path), cb

req_post = (path, data, cb) ->
  options =
    uri: abs_url(path)
    form: data
  request.post options, cb

describeTests = () ->
  app = require '../app'
  describeAppTests config.storageType, app

# Describes app tests for the given storage type and app.
describeAppTests = (type, app) ->

  describe "App with #{type} store", ->

    before (done) ->
      app.argumenta.storage.clearAll null, (err) ->
        should.not.exist err
        done()

    describe '/', () ->
      describe 'GET /', ->
        it 'should respond with index and links to log in', (done) ->
          get '/', (res) ->
            res.status.should.equal 200
            res.text.should.match /Argumenta/
            res.text.should.match /Sign in.*or.*Join now!/
            done()

    describe '/users', () ->

      describe 'POST /users', () ->
        it 'should create a new user and sign in', (done) ->
          user =
            username: 'tester'
            password: '<PASSWORD>'
            email:    '<EMAIL>'
          post '/users', user, (err, res) ->
            res.status.should.equal 200
            res.text.should.match /tester/
            res.text.should.match new RegExp 'Logged in as <a href="/tester">tester</a>'
            done()

        it 'should refuse to create an invalid user', (done) ->
{'fraction_non_alphanumeric': 0.1357950565395777, 'fraction_numerical': 0.005603922745922145, 'mean_word_length': 2.6769683590875646, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 99, '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': '313037', 'n_tokens_mistral': 3140, 'n_tokens_neox': 3059, 'n_words': 1004}