<commit_before>require 'test_helper'

module Shipit
  module Api
    class HooksControllerTest < ActionController::TestCase
      setup do
        authenticate!
        @stack = shipit_stacks(:shipit)
      end

      test "the route has priority over stacks one" do
        assert_recognizes({controller: 'shipit/api/hooks', action: 'show', id: '42' }, '/api/hooks/42')
      end

      test "#index without a stack_id returns the list of global hooks" do
        hook = Hook.global.first

        get :index
        assert_response :ok
        assert_json '0.id', hook.id
        assert_json '0.delivery_url', hook.delivery_url
        assert_json '0.content_type', hook.content_type
        assert_no_json '0.stack'
      end

      test "#index with a stack_id returns the list of scoped hooks" do
        hook = Hook.scoped_to(@stack).first

        get :index, stack_id: @stack.to_param
        assert_response :ok
        assert_json '0.id', hook.id
        assert_json '0.delivery_url', hook.delivery_url
        assert_json '0.content_type', hook.content_type
        assert_json '0.stack.id', @stack.id
      end

      test "#show returns the hooks" do
        hook = Hook.scoped_to(@stack).first

        get :show, stack_id: @stack.to_param, id: hook.id
        assert_response :ok

        assert_json 'id', hook.id
        assert_json 'delivery_url', hook.delivery_url
        assert_json 'content_type', hook.content_type
        assert_json 'stack.id', @stack.id
      end

      test "#create adds a new hook" do
        assert_difference -> { Hook.count }, 1 do
          post :create, delivery_url: 'https://example.com/hook', events: %w(deploy rollback)
        end
        hook = Hook.last
        assert_json 'delivery_url', 'https://example.com/hook'
        assert_json 'url', "http://shipit.com/api/hooks/#{hook.id}"
        assert_json 'id', hook.id
      end

      test "#create do not allow to set protected attributes" do
        post :create, delivery_url: 'https://example.com/hook', events: %w(deploy rollback), created_at: 2.months.ago.to_s(:db)<commit_msg>Fixed style "issues".<commit_after>require 'test_helper'

module Shipit
  module Api
    class HooksControllerTest < ActionController::TestCase
      setup do
        authenticate!
        @stack = shipit_stacks(:shipit)
      end

      test "the route has priority over stacks one" do
        assert_recognizes({controller: 'shipit/api/hooks', action: 'show', id: '42'}, '/api/hooks/42')
      end

      test "#index without a stack_id returns the list of global hooks" do
        hook = Hook.global.first

        get :index
        assert_response :ok
        assert_json '0.id', hook.id
        assert_json '0.delivery_url', hook.delivery_url
        assert_json '0.content_type', hook.content_type
        assert_no_json '0.stack'
      end

      test "#index with a stack_id returns the list of scoped hooks" do
        hook = Hook.scoped_to(@stack).first

        get :index, stack_id: @stack.to_param
        assert_response :ok
        assert_json '0.id', hook.id
        assert_json '0.delivery_url', hook.delivery_url
        assert_json '0.content_type', hook.content_type
        assert_json '0.stack.id', @stack.id
      end

      test "#show returns the hooks" do
        hook = Hook.scoped_to(@stack).first

        get :show, stack_id: @stack.to_param, id: hook.id
        assert_response :ok

        assert_json 'id', hook.id
        assert_json 'delivery_url', hook.delivery_url
        assert_json 'content_type', hook.content_type
        assert_json 'stack.id', @stack.id
      end

      test "#create adds a new hook" do
        assert_difference -> { Hook.count }, 1 do
          post :create, delivery_url: 'https://example.com/hook', events: %w(deploy rollback)
        end
        hook = Hook.last
        assert_json 'delivery_url', 'https://example.com/hook'
        assert_json 'url', "http://shipit.com/api/hooks/#{hook.id}"
        assert_json 'id', hook.id
      end

      test "#create do not allow to set protected attributes" do
        post :create, delivery_url: 'https://example.com/hook',
                      events: %w(deploy rollback),
                      created_at: 2.months.ago.to_s(:db)
{'fraction_non_alphanumeric': 0.10460448642266824, 'fraction_numerical': 0.006611570247933884, 'mean_word_length': 2.4133763094278806, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, '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': '4883374', 'n_tokens_mistral': 1312, 'n_tokens_neox': 1276, 'n_words': 349}