<commit_before>{mk-pgrest-fortest,create-test-table,cleanup-test-table} = require \./testlib

var plx
describe 'Protected resources', ->
  @timeout 10000ms
  beforeEach (done) ->
    _plx <- mk-pgrest-fortest meta:
      pgrest_protected:
        as: 'pgrest_test'
        rules: [
          * name: \pgrest_update
            event: \insert
            type: \also
            command: """
              SELECT ~> 'throw 403'
            """
        ]

    plx := _plx
    <- create-test-table plx
    pgrest = require \..
    <- pgrest.bootstrap plx, \dummy, process.cwd! + \/test/dummy.json
    done!
  afterEach (done) ->
    <- cleanup-test-table plx
    done!
  describe 'creates view' (,)-> it
    .. 'should have view', (done) ->
      res <- plx.query """select pgrest_select($1)""", [collection: \pgrest_protected]
      res.0.should.have.keys 'pgrest_select'
      res.0.pgrest_select.paging.count.should.eql 0
      done!
    .. 'should be able to insert to raw', (done) ->
      res <- plx.query """select pgrest_insert($1)""", [collection: \pgrest_test, $: [
        * field: \a, value: <[a b]>
      ]]
      res <- plx.query """select pgrest_select($1)""", [collection: \pgrest_protected]
      res.0.should.have.keys 'pgrest_select'
      res.0.pgrest_select.paging.count.should.eql 1
      done!
    .. 'should throw for protected resources', (done) ->
      <- plx.insert collection: \pgrest_protected, $: [
        * field: \a, value: <[a b]>
      ], _, -> it.should.match /403/; done!
      it.should.eq null

describe 'Protected resources', ->
  @timeout 10000ms
  beforeEach (done) ->
    _plx <- mk-pgrest-fortest meta:
      pgrest_protected:
        as: 'pgrest_test'
        rules: [
          * name: \pgrest_update
            event: \insert
            type: \also
            command: """
              SELECT ~> $$throw 403 unless require('pgrest').pgrest_param_get('auth') is 'secret'$$
            """
        ]

    plx := _plx
    <- create-test-table plx
    pgrest = require \..
    <- pgrest.bootstrap plx, \dummy, process.cwd! + \/test/dummy.json
    done!
  afterEach (done) ->
    #<- cleanup-test-table plx
    done!
  describe 'simple view is updatable' (,)-> it
    .. 'denied by default', (done) ->
      <- plx.insert collection: \pgrest_protected, $: [
        * field: \a, value: <[a b]>
      ], _, -> it.should.match /403/; done!
      it.should.eq null
    .. 'allowed with secret key', (done) ->
      rows <- plx.query "select version()"
      [_, pg_version] = rows.0.version.match /^PostgreSQL ([\d\.]+)/
      if pg_version < \9.3.0
        it.skip 'skipped for < 9.3', ->
        return done!

      res <- plx.insert collection: \pgrest_protected, pgparam: {auth: \secret}, $: [
        * field: \a, value: <[a b]>
      ]
      res.should.be.deep.eq [1]
      done!
<commit_msg>skip updatable view tests for pg < 9.3<commit_after>{mk-pgrest-fortest,create-test-table,cleanup-test-table} = require \./testlib

var plx
describe 'Protected resources', ->
  @timeout 10000ms
  beforeEach (done) ->
    _plx <- mk-pgrest-fortest meta:
      pgrest_protected:
        as: 'pgrest_test'
        rules: [
          * name: \pgrest_update
            event: \insert
            type: \also
            command: """
              SELECT ~> 'throw 403'
            """
        ]

    plx := _plx
    <- create-test-table plx
    pgrest = require \..
    <- pgrest.bootstrap plx, \dummy, process.cwd! + \/test/dummy.json
    done!
  afterEach (done) ->
    <- cleanup-test-table plx
    done!
  describe 'creates view' (,)-> it
    .. 'should have view', (done) ->
      res <- plx.query """select pgrest_select($1)""", [collection: \pgrest_protected]
      res.0.should.have.keys 'pgrest_select'
      res.0.pgrest_select.paging.count.should.eql 0
      done!
    .. 'should be able to insert to raw', (done) ->
      res <- plx.query """select pgrest_insert($1)""", [collection: \pgrest_test, $: [
        * field: \a, value: <[a b]>
      ]]
      res <- plx.query """select pgrest_select($1)""", [collection: \pgrest_protected]
      res.0.should.have.keys 'pgrest_select'
      res.0.pgrest_select.paging.count.should.eql 1
      done!
    .. 'should throw for protected resources', (done) ->
      rows <- plx.query "select version()"
      [_, pg_version] = rows.0.version.match /^PostgreSQL ([\d\.]+)/
      if pg_version < \9.3.0
        it.skip 'skipped for < 9.3', ->
        return done!

      <- plx.insert collection: \pgrest_protected, $: [
        * field: \a, value: <[a b]>
      ], _, -> it.should.match /403/; done!
      it.should.eq null

describe 'Protected resources', ->
  @timeout 10000ms
  beforeEach (done) ->
    _plx <- mk-pgrest-fortest meta:
      pgrest_protected:
        as: 'pgrest_test'
        rules: [
          * name: \pgrest_update
            event: \insert
            type: \also
            command: """
              SELECT ~> $$throw 403 unless require('pgrest').pgrest_param_get('auth') is 'secret'$$
            """
        ]

    plx := _plx
    <- create-test-table plx
    pgrest = require \..
    <- pgrest.bootstrap plx, \dummy, process.cwd! + \/test/dummy.json
    done!
  afterEach (done) ->
    #<- cleanup-test-table plx
    done!
  describe 'simple view is updatable' (,)-> it
    .. 'denied by default', (done) ->
      rows <- plx.query "select version()"
      [_, pg_version] = rows.0.version.match /^PostgreSQL ([\d\.]+)/
      if pg_version < \9.3.0
        it.skip 'skipped for < 9.3', ->
        return done!

      <- plx.insert collection: \pgrest_protected, $: [
        * field: \a, value: <[a b]>
      ], _, -> it.should.match /403/; done!
      it.should.eq null
    .. 'allowed with secret key', (done) ->
      rows <- plx.query "select version()"
      [_, pg_version] = rows.0.version.match /^PostgreSQL ([\d\.]+)/
      if pg_version < \9.3.0
        it.skip 'skipped for < 9.3', ->
        return done!
      res <- plx.insert collection: \pgrest_protected, pgparam: {auth: \secret}, $: [
        * field: \a, value: <[a b]>
      ]
      res.should.be.deep.eq [1]
      done!

{'fraction_non_alphanumeric': 0.16306483300589392, 'fraction_numerical': 0.014734774066797643, 'mean_word_length': 1.9741966893865628, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 49, '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': '5857231', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2058, 'n_words': 478}