<commit_before>require 'spec_helper'

describe API::API do
  include ApiHelpers

  let(:gitlab_url) { GitlabCi.config.gitlab_server_urls.first }
  let(:auth_opts) {
    {
      :email => "<EMAIL>",
      :password => "<PASSWORD>"
    }
  }
  let(:private_token) { Network.new.authenticate(gitlab_url, auth_opts)["private_token"] }

  let(:options) {
    {
      :private_token => private_token,
      :url => gitlab_url
    }
  }
  
  before {
    stub_gitlab_calls
  }
  
  context "requests for scoped projects" do
    # NOTE: These ids are tied to the actual projects on demo.gitlab.com
    describe "GET /projects" do
      let!(:project1) { FactoryGirl.create(:project, :name => "gitlabhq", :gitlab_id => 3) }
      let!(:project2) { FactoryGirl.create(:project, :name => "gitlab-ci", :gitlab_id => 4) }

      it "should return all projects on the CI instance" do
        get api("/projects"), options
        response.status.should == 200
        json_response.count.should == 2
        json_response.first["id"].should == project1.id
        json_response.last["id"].should == project2.id
      end
    end

    describe "GET /projects/owned" do
      # NOTE: This user doesn't own any of these projects on demo.gitlab.com
      let!(:project1) { FactoryGirl.create(:project, :name => "gitlabhq", :gitlab_id => 3) }
      let!(:project2) { FactoryGirl.create(:project, :name => "random-project", :gitlab_id => 9898) }

      it "should return all projects on the CI instance" do
        get api("/projects/owned"), options

        response.status.should == 200
        json_response.count.should == 0
      end
    end
  end


  describe "GET /projects/:id" do
    let!(:project) { FactoryGirl.create(:project) }

    context "with an existing project" do
      it "should retrieve the project info" do
        get api("/projects/#{project.id}"), options
        response.status.should == 200
        json_response['id'].should == project.id
      end
    end

    context "with a non-existing project" do
      it "should return 404 error if project not found" do
        get api("/projects/non_existent_id"), options
        response.status.should == 404
      end
    end
  end

  describe "PUT /projects/:id" do
    let!(:project) { FactoryGirl.create(:project) }<commit_msg>Tests for Projects API method for webhooks creation<commit_after>require 'spec_helper'

describe API::API do
  include ApiHelpers

  let(:gitlab_url) { GitlabCi.config.gitlab_server_urls.first }
  let(:auth_opts) {
    {
      :email => "<EMAIL>",
      :password => "<PASSWORD>"
    }
  }
  let(:private_token) { Network.new.authenticate(gitlab_url, auth_opts)["private_token"] }

  let(:options) {
    {
      :private_token => <PASSWORD>_token,
      :url => gitlab_url
    }
  }
  
  before {
    stub_gitlab_calls
  }
  
  context "requests for scoped projects" do
    # NOTE: These ids are tied to the actual projects on demo.gitlab.com
    describe "GET /projects" do
      let!(:project1) { FactoryGirl.create(:project, :name => "gitlabhq", :gitlab_id => 3) }
      let!(:project2) { FactoryGirl.create(:project, :name => "gitlab-ci", :gitlab_id => 4) }

      it "should return all projects on the CI instance" do
        get api("/projects"), options
        response.status.should == 200
        json_response.count.should == 2
        json_response.first["id"].should == project1.id
        json_response.last["id"].should == project2.id
      end
    end

    describe "GET /projects/owned" do
      # NOTE: This user doesn't own any of these projects on demo.gitlab.com
      let!(:project1) { FactoryGirl.create(:project, :name => "gitlabhq", :gitlab_id => 3) }
      let!(:project2) { FactoryGirl.create(:project, :name => "random-project", :gitlab_id => 9898) }

      it "should return all projects on the CI instance" do
        get api("/projects/owned"), options

        response.status.should == 200
        json_response.count.should == 0
      end
    end
  end

  describe "POST /projects/:project_id/webhooks" do
    let!(:project) { FactoryGirl.create(:project) }

    context "Valid Webhook URL" do
      let!(:webhook) { {:web_hook => "http://example.com/sth/1/ala_ma_kota" } }

      before do
        options.merge!(webhook)
      end

      it "should create webhook for specified project" do
        post api("/projects/#{project.id}/webhooks"), options
        response.status.should == 201
        json_response["url"].should == webhook[:web_hook]
      end

      it "fails to create webhook for non existsing project" do
        post api("/projects/non-existant-id/webhooks"), options
        response.status.should == 404
      end

    end

    context "Invalid Webhook URL" do
      let!(:webhook) { {:web_hook => "ala_ma_kota" } }

      before do
        options.merge!(webhook)
      end

      it "fails to create webhook for not valid url" do
        post api("/projects/#{project.id}/webhooks"), options
        response.status.should == 400
      end
    end

    context "Missed web_hook parameter" do
      it "fails to create webhook for not provided url" do
        post api("/projects/#{project.id}/webhooks"), options
        response.status.should == 400
      end
    end
  end

  describe "GET /projects/:id" do
    let!(:project) { FactoryGirl.create(:project) }

    context "with an existing project" do
      it "should retrieve the project info" do
        get api("/projects/#{project.id}"), options
        response.status.should == 200
        json_response['id'].should == project.id
      end
    end

    context "with a non-existing project" do
      it "should return 404 error if project not found" do
        get api("/projects/non_existent_id"), options
        response.status.should == 404
      end
    end
  end

  describe "PUT /projects/:id" do
    let!(:project) { FactoryGirl.create(:project) }
{'fraction_non_alphanumeric': 0.12399795326624595, 'fraction_numerical': 0.012450963670475866, 'mean_word_length': 2.709044908285895, 'pattern_counts': {'":': 0, '<': 8, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '108894', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1770, 'n_words': 480}