describe Travis::API::V3::Services::Jobs::Find, set_app: true do
  include Support::Formats
  let(:repo)   { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first }
  let(:build)  { repo.builds.first }
  let(:stages) { build.stages }
  let(:jobs)   { Travis::API::V3::Models::Build.find(build.id).jobs }
  let(:commit) { build.commit }
  let(:parsed_body) { JSON.load(body) }

  before do
    # TODO should this go into the scenario? is it ok to keep it here?
    test   = build.stages.create(number: 1, name: 'test')
    deploy = build.stages.create(number: 2, name: 'deploy')
    jobs[0, 2].each { |job| job.update!(stage: test) }
    jobs[2, 2].each { |job| job.update!(stage: deploy) }
    jobs.each(&:reload)
  end

  let(:authorization) { { 'permissions' => ['repository_state_update', 'repository_build_create', 'repository_settings_create', 'repository_settings_update', 'repository_cache_view', 'repository_cache_delete', 'repository_settings_delete', 'repository_log_view', 'repository_log_delete', 'repository_build_cancel', 'repository_build_debug', 'repository_build_restart', 'repository_settings_read', 'repository_scans_view'] } }

  before { stub_request(:get, %r((.+)/permissions/repo/(.+))).to_return(status: 200, body: JSON.generate(authorization)) }

  describe "jobs on public repository" do

    let(:authorization) { { 'permissions' => ['repository_log_view', 'repository_settings_read'] } }
    before     { get("/v3/build/#{build.id}/jobs?include=job.config") }
    example    { expect(last_response).to be_ok }
    example    { expect(parsed_body).to eql_json({
      "@type"                   => "jobs",
      "@href"                   => "/v3/build/#{build.id}/jobs?include=job.config",
      "@representation"         => "standard",
      "jobs"                    => [{
        "@type"                 => "job",
        "@href"                 => "/v3/job/#{jobs[0].id}",
        "@representation"       => "standard",
        "@permissions"          => {
          "read"                => true,
          "cancel"              => false,
          "restart"             => false,
          "debug"               => false,
          "delete_log"          => false,
          "view_log"            => true,
          "prioritize"          => false },
        "id"                    => jobs[0].id,
        "private"          => false,
        "restarted_at"          => nil,
        "restarted_by"          => nil,
        "number"                => "#{jobs[0].number}",
        "state"                 => "configured",
        "started_at"            => "2010-11-12T13:00:00Z",
        "finished_at"           => nil,
        "allow_failure"         => jobs[0].allow_failure,
        "created_at"            => json_format_time_with_ms(jobs[0].created_at),
        "updated_at"            => json_format_time_with_ms(jobs[0].updated_at),
        "vm_size"               => nil,
        "config"                => {
          "rvm"                 => "1.8.7",
          "gemfile"             => "test/Gemfile.rails-2.3.x",
          "language"            => "ruby",
          "group"               => "stable",
          "dist"                => "precise",
          "os"                  => "linux" },
        "build"                 => {
          "@type"               => "build",
          "@href"               => "/v3/build/#{build.id}",
          "@representation"     => "minimal",
          "id"                  => build.id,
          "private"        => false,
          "priority"            => false,
          "number"              => build.number,
          "state"               => "configured",
          "duration"            => nil,
          "event_type"          => "push",
          "previous_state"      => "passed",
          "pull_request_number" => build.pull_request_number,
          "pull_request_title"  => build.pull_request_title,
          "started_at"          => "2010-11-12T13:00:00Z",
          "finished_at"         => nil},
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[0].id,
          "number"              => 1,
          "name"                => "test",
          "state"               => stages[0].state,
          "started_at"          => stages[0].started_at,
          "finished_at"         => stages[0].finished_at},
        "queue"                 => "builds.linux",
        "repository"            => {
          "@type"               => "repository",
          "@href"               => "/v3/repo/1",
          "@representation"     => "minimal",
          "id"                  => repo.id,
          "name"                => "minimal",
          "slug"                => "svenfuchs/minimal"},
        "commit"                => {
          "@type"               => "commit",
          "@representation"     => "minimal",
          "id"                  => commit.id,
          "sha"                 => commit.commit,
          "ref"                 => commit.ref,
          "message"             => commit.message,
          "compare_url"         => commit.compare_url,
          "committed_at"        => "2010-11-12T12:55:00Z"},
        "owner"                 => {
          "@type"               => "user",
          "@href"               => "/v3/user/1",
          "@representation"     => "minimal",
          "id"                  => 1,
          "vcs_type"            => 'GithubUser',
          "ro_mode"             => false,
          "login"               => "svenfuchs",
          "name"                => "Sven Fuchs"}},
        {"@type"                => "job",
        "@href"                 => "/v3/job/#{jobs[1].id}",
        "@representation"       => "standard",
        "@permissions"          => {
          "read"                => true,
          "cancel"              => false,
          "restart"             => false,
          "debug"               => false,
          "delete_log"          => false,
          "view_log"            => true,
          "prioritize"          => false},
        "id"                    => jobs[1].id,
        "private"          => false,
        "restarted_at"          => nil,
        "restarted_by"          => nil,
        "number"                => "#{jobs[1].number}",
        "state"                 => "configured",
        "started_at"            => "2010-11-12T13:00:00Z",
        "finished_at"           => nil,
        "allow_failure"         => jobs[1].allow_failure,
        "created_at"            => json_format_time_with_ms(jobs[1].created_at),
        "updated_at"            => json_format_time_with_ms(jobs[1].updated_at),
        "vm_size"               => nil,
        "config"                => {
          "rvm"                 => "1.8.7",
          "gemfile"             => "test/Gemfile.rails-3.0.x",
          "language"            => "ruby",
          "group"               => "stable",
          "dist"                => "precise",
          "os"                  => "linux" },
        "build"                 => {
          "@type"               => "build",
          "@href"               => "/v3/build/#{build.id}",
          "@representation"     => "minimal",
          "id"                  => build.id,
          "private"        => false,
          "priority"            => false,
          "number"              => build.number,
          "state"               => "configured",
          "duration"            => nil,
          "event_type"          => "push",
          "previous_state"      => "passed",
          "pull_request_number" => nil,
          "pull_request_title"  => nil,
          "started_at"          => "2010-11-12T13:00:00Z",
          "finished_at"         => nil},
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[0].id,
          "number"              => 1,
          "name"                => "test",
          "state"               => stages[0].state,
          "started_at"          => stages[0].started_at,
          "finished_at"         => stages[0].finished_at},
        "queue"                 => "builds.linux",
        "repository"            => {
          "@type"               => "repository",
          "@href"               => "/v3/repo/1",
          "@representation"     => "minimal",
          "id"                  => repo.id,
          "name"                => "minimal",
          "slug"                => "svenfuchs/minimal"},
        "commit"                => {
          "@type"               => "commit",
          "@representation"     => "minimal",
          "id"                  => commit.id,
          "sha"                 => commit.commit,
          "ref"                 => commit.ref,
          "message"             => commit.message,
          "compare_url"         => commit.compare_url,
          "committed_at"        => "2010-11-12T12:55:00Z"},
        "owner"                 => {
          "@type"               => "user",
          "@href"               => "/v3/user/1",
          "@representation"     => "minimal",
          "id"                  => 1,
          "vcs_type"            => 'GithubUser',
          "ro_mode"             => false,
          "login"               => "svenfuchs",
          "name"                => "Sven Fuchs"}},
        {"@type"                => "job",
        "@href"                 => "/v3/job/#{jobs[2].id}",
        "@representation"       => "standard",
        "@permissions"          => {
          "read"                => true,
          "cancel"              => false,
          "restart"             => false,
          "debug"               => false,
          "delete_log"          => false,
          "view_log"            => true,
          "prioritize"          => false},
        "id"                    => jobs[2].id,
        "private"          => false,
        "restarted_at"          => nil,
        "restarted_by"          => nil,
        "number"                => "#{jobs[2].number}",
        "state"                 => "configured",
        "started_at"            => "2010-11-12T13:00:00Z",
        "finished_at"           => nil,
        "allow_failure"         => jobs[2].allow_failure,
        "created_at"            => json_format_time_with_ms(jobs[2].created_at),
        "updated_at"            => json_format_time_with_ms(jobs[2].updated_at),
        "vm_size"               => nil,
        "config"                => {
          "rvm"                 => "1.9.2",
          "gemfile"             => "test/Gemfile.rails-2.3.x",
          "language"            => "ruby",
          "group"               => "stable",
          "dist"                => "precise",
          "os"                  => "linux" },
        "build"                 => {
          "@type"               => "build",
          "@href"               => "/v3/build/#{build.id}",
          "@representation"     => "minimal",
          "id"                  => build.id,
          "private"        => false,
          "priority"            => false,
          "number"              => build.number,
          "state"               => "configured",
          "duration"            => nil,
          "event_type"          => "push",
          "previous_state"      => "passed",
          "pull_request_number" => build.pull_request_number,
          "pull_request_title"  => build.pull_request_title,
          "started_at"          => "2010-11-12T13:00:00Z",
          "finished_at"         => nil},
        "queue"                 => "builds.linux",
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[1].id,
          "number"              => 2,
          "name"                => "deploy",
          "state"               => stages[1].state,
          "started_at"          => stages[1].started_at,
          "finished_at"         => stages[1].finished_at},
        "repository"            => {
          "@type"               => "repository",
          "@href"               => "/v3/repo/1",
          "@representation"     => "minimal",
          "id"                  => repo.id,
          "name"                => "minimal",
          "slug"                => "svenfuchs/minimal"},
        "commit"                => {
          "@type"               => "commit",
          "@representation"     => "minimal",
          "id"                  => commit.id,
          "sha"                 => commit.commit,
          "ref"                 => commit.ref,
          "message"             => commit.message,
          "compare_url"         => commit.compare_url,
          "committed_at"        => "2010-11-12T12:55:00Z"},
        "owner"                 => {
          "@type"               => "user",
          "@href"               => "/v3/user/1",
          "@representation"     => "minimal",
          "id"                  => 1,
          "vcs_type"            => 'GithubUser',
          "ro_mode"             => false,
          "login"               => "svenfuchs",
          "name"                => "Sven Fuchs"}},
        {"@type"                => "job",
        "@href"                 => "/v3/job/#{jobs[3].id}",
        "@representation"       => "standard",
        "@permissions"          => {
          "read"                => true,
          "cancel"              => false,
          "restart"             => false,
          "debug"               => false,
          "delete_log"          => false,
          "view_log"            => true,
          "prioritize"          => false},
        "id"                    => jobs[3].id,
        "private"          => false,
        "restarted_at"          => nil,
        "restarted_by"          => nil,
        "number"                => "#{jobs[3].number}",
        "state"                 => "configured",
        "started_at"            => "2010-11-12T13:00:00Z",
        "finished_at"           => nil,
        "allow_failure"         => jobs[3].allow_failure,
        "created_at"            => json_format_time_with_ms(jobs[3].created_at),
        "updated_at"            => json_format_time_with_ms(jobs[3].updated_at),
        "vm_size"               => nil,
        "config"                => {
          "rvm"                 => "1.9.2",
          "gemfile"             => "test/Gemfile.rails-3.0.x",
          "language"            => "ruby",
          "group"               => "stable",
          "dist"                => "precise",
          "os"                  => "linux" },
        "build"                 => {
          "@type"               => "build",
          "@href"               => "/v3/build/#{build.id}",
          "@representation"     => "minimal",
          "id"                  => build.id,
          "private"        => false,
          "priority"            => false,
          "number"              => build.number,
          "state"               => "configured",
          "duration"            => nil,
          "event_type"          => "push",
          "previous_state"      => "passed",
          "pull_request_number" => build.pull_request_number,
          "pull_request_title"  => build.pull_request_title,
          "started_at"          => "2010-11-12T13:00:00Z",
          "finished_at"         => nil},
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[1].id,
          "number"              => 2,
          "name"                => "deploy",
          "state"               => stages[1].state,
          "started_at"          => stages[1].started_at,
          "finished_at"         => stages[1].finished_at},
        "queue"                 => "builds.linux",
        "repository"            => {
          "@type"               => "repository",
          "@href"               => "/v3/repo/1",
          "@representation"     => "minimal",
          "id"                  => repo.id,
          "name"                => "minimal",
          "slug"                => "svenfuchs/minimal"},
        "commit"                => {
          "@type"               => "commit",
          "@representation"     => "minimal",
          "id"                  => commit.id,
          "sha"                 => commit.commit,
          "ref"                 => commit.ref,
          "message"             => commit.message,
          "compare_url"         => commit.compare_url,
          "committed_at"        => "2010-11-12T12:55:00Z"},
        "owner"                 => {
          "@type"               => "user",
          "@href"               => "/v3/user/1",
          "@representation"     => "minimal",
          "id"                  => 1,
          "vcs_type"            => 'GithubUser',
          "ro_mode"             => false,
          "login"               => "svenfuchs",
          "name"                => "Sven Fuchs"}}
        ]
      })
    }
  end

  describe "jobs private repository, private API, authenticated as user with pull access" do

  let(:authorization) { { 'permissions' => ['repository_log_view', 'repository_build_cancel', 'repository_build_restart', 'repository_settings_read', 'repository_scans_view'] } }
    let(:token)   { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
    let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}"                        }}
    before        { Travis::API::V3::Models::Permission.create(repository: repo, user: repo.owner, pull: true) }
    before        { repo.update_attribute(:private, true)                             }
    before        { get("/v3/build/#{build.id}/jobs?include=job.config", {}, headers)                           }
    after         { repo.update_attribute(:private, false)                            }
    example       { expect(last_response).to be_ok                                    }
    example    { expect(parsed_body).to eql_json({
      "@type"                   => "jobs",
      "@href"                   => "/v3/build/#{build.id}/jobs?include=job.config",
      "@representation"         => "standard",
      "jobs"                    => [{
        "@type"                 => "job",
        "@href"                 => "/v3/job/#{jobs[0].id}",
        "@representation"       => "standard",
        "@permissions"          => {
          "read"                => true,
          "cancel"              => true,
          "restart"             => true,
          "debug"               => false,
          "delete_log"          => false,
          "view_log"            => true,
          "prioritize"          => false },
        "id"                    => jobs[0].id,
        "private"          => false,
        "restarted_at"          => nil,
        "restarted_by"          => nil,
        "number"                => "#{jobs[0].number}",
        "state"                 => "configured",
        "started_at"            => "2010-11-12T13:00:00Z",
        "finished_at"           => nil,
        "allow_failure"         => jobs[0].allow_failure,
        "created_at"            => json_format_time_with_ms(jobs[0].created_at),
        "updated_at"            => json_format_time_with_ms(jobs[0].updated_at),
        "vm_size"               => nil,
        "config"                => {
          "rvm"                 => "1.8.7",
          "gemfile"             => "test/Gemfile.rails-2.3.x",
          "language"            => "ruby",
          "group"               => "stable",
          "dist"                => "precise",
          "os"                  => "linux" },
        "build"                 => {
          "@type"               => "build",
          "@href"               => "/v3/build/#{build.id}",
          "@representation"     => "minimal",
          "id"                  => build.id,
          "private"        => false,
          "priority"            => false,
          "number"              => build.number,
          "state"               => "configured",
          "duration"            => nil,
          "event_type"          => "push",
          "previous_state"      => "passed",
          "pull_request_number" => build.pull_request_number,
          "pull_request_title"  => build.pull_request_title,
          "started_at"          => "2010-11-12T13:00:00Z",
          "finished_at"         => nil},
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[0].id,
          "number"              => 1,
          "name"                => "test",
          "state"               => stages[0].state,
          "started_at"          => stages[0].started_at,
          "finished_at"         => stages[0].finished_at},
        "queue"                 => "builds.linux",
        "repository"            => {
          "@type"               => "repository",
          "@href"               => "/v3/repo/1",
          "@representation"     => "minimal",
          "id"                  => repo.id,
          "name"                => "minimal",
          "slug"                => "svenfuchs/minimal"},
        "commit"                => {
          "@type"               => "commit",
          "@representation"     => "minimal",
          "id"                  => commit.id,
          "sha"                 => commit.commit,
          "ref"                 => commit.ref,
          "message"             => commit.message,
          "compare_url"         => commit.compare_url,
          "committed_at"        => "2010-11-12T12:55:00Z"},
        "owner"                 => {
          "@type"               => "user",
          "@href"               => "/v3/user/1",
          "@representation"     => "minimal",
          "id"                  => 1,
          "vcs_type"            => 'GithubUser',
          "ro_mode"             => true,
          "login"               => "svenfuchs",
          "name"                => "Sven Fuchs"}},
        {"@type"                => "job",
        "@href"                 => "/v3/job/#{jobs[1].id}",
        "@representation"       => "standard",
        "@permissions"          => {
          "read"                => true,
          "cancel"              => true,
          "restart"             => true,
          "debug"               => false,
          "delete_log"          => false,
          "view_log"            => true,
          "prioritize"          => false },
        "id"                    => jobs[1].id,
        "private"          => false,
        "restarted_at"          => nil,
        "restarted_by"          => nil,
        "number"                => "#{jobs[1].number}",
        "state"                 => "configured",
        "started_at"            => "2010-11-12T13:00:00Z",
        "allow_failure"         => jobs[1].allow_failure,
        "created_at"            => json_format_time_with_ms(jobs[1].created_at),
        "updated_at"            => json_format_time_with_ms(jobs[1].updated_at),
        "vm_size"               => nil,
        "config"                => {
          "rvm"                 => "1.8.7",
          "gemfile"             => "test/Gemfile.rails-3.0.x",
          "language"            => "ruby",
          "group"               => "stable",
          "dist"                => "precise",
          "os"                  => "linux" },
        "finished_at"           => nil,
        "build"                 => {
          "@type"               => "build",
          "@href"               => "/v3/build/#{build.id}",
          "@representation"     => "minimal",
          "id"                  => build.id,
          "private"        => false,
          "priority"            => false,
          "number"              => build.number,
          "state"               => "configured",
          "duration"            => nil,
          "event_type"          => "push",
          "previous_state"      => "passed",
          "pull_request_number" => build.pull_request_number,
          "pull_request_title"  => build.pull_request_title,
          "started_at"          => "2010-11-12T13:00:00Z",
          "finished_at"         => nil},
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[0].id,
          "number"              => 1,
          "name"                => "test",
          "state"               => stages[0].state,
          "started_at"          => stages[0].started_at,
          "finished_at"         => stages[0].finished_at},
        "queue"                 => "builds.linux",
        "repository"            => {
          "@type"               => "repository",
          "@href"               => "/v3/repo/1",
          "@representation"     => "minimal",
          "id"                  => repo.id,
          "name"                => "minimal",
          "slug"                => "svenfuchs/minimal"},
        "commit"                => {
          "@type"               => "commit",
          "@representation"     => "minimal",
          "id"                  => commit.id,
          "sha"                 => commit.commit,
          "ref"                 => commit.ref,
          "message"             => commit.message,
          "compare_url"         => commit.compare_url,
          "committed_at"        => "2010-11-12T12:55:00Z"},
        "owner"                 => {
          "@type"               => "user",
          "@href"               => "/v3/user/1",
          "@representation"     => "minimal",
          "id"                  => 1,
          "vcs_type"            => 'GithubUser',
          "ro_mode"             => true,
          "login"               => "svenfuchs",
          "name"                => "Sven Fuchs"}},
        {"@type"                => "job",
        "@href"                 => "/v3/job/#{jobs[2].id}",
        "@representation"       => "standard",
        "@permissions"          => {
          "read"                => true,
          "cancel"              => true,
          "restart"             => true,
          "debug"               => false,
          "delete_log"          => false,
          "view_log"            => true,
          "prioritize"          => false },
        "id"                    => jobs[2].id,
        "private"          => false,
        "restarted_at"          => nil,
        "restarted_by"          => nil,
        "number"                => "#{jobs[2].number}",
        "state"                 => "configured",
        "started_at"            => "2010-11-12T13:00:00Z",
        "finished_at"           => nil,
        "allow_failure"         => jobs[2].allow_failure,
        "created_at"            => json_format_time_with_ms(jobs[2].created_at),
        "updated_at"            => json_format_time_with_ms(jobs[2].updated_at),
        "vm_size"               => nil,
        "config"                => {
          "rvm"                 => "1.9.2",
          "gemfile"             => "test/Gemfile.rails-2.3.x",
          "language"            => "ruby",
          "group"               => "stable",
          "dist"                => "precise",
          "os"                  => "linux" },
        "build"                 => {
          "@type"               => "build",
          "@href"               => "/v3/build/#{build.id}",
          "@representation"     => "minimal",
          "id"                  => build.id,
          "private"        => false,
          "priority"            => false,
          "number"              => build.number,
          "state"               => "configured",
          "duration"            => nil,
          "event_type"          => "push",
          "previous_state"      => "passed",
          "pull_request_number" => build.pull_request_number,
          "pull_request_title"  => build.pull_request_title,
          "started_at"          => "2010-11-12T13:00:00Z",
          "finished_at"         => nil},
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[1].id,
          "number"              => 2,
          "name"                => "deploy",
          "state"               => stages[1].state,
          "started_at"          => stages[1].started_at,
          "finished_at"         => stages[1].finished_at},
        "queue"                 => "builds.linux",
        "repository"            => {
          "@type"               => "repository",
          "@href"               => "/v3/repo/1",
          "@representation"     => "minimal",
          "id"                  => repo.id,
          "name"                => "minimal",
          "slug"                => "svenfuchs/minimal"},
        "commit"                => {
          "@type"               => "commit",
          "@representation"     => "minimal",
          "id"                  => commit.id,
          "sha"                 => commit.commit,
          "ref"                 => commit.ref,
          "message"             => commit.message,
          "compare_url"         => commit.compare_url,
          "committed_at"        => "2010-11-12T12:55:00Z"},
        "owner"                 => {
          "@type"               => "user",
          "@href"               => "/v3/user/1",
          "@representation"     => "minimal",
          "id"                  => 1,
          "vcs_type"            => 'GithubUser',
          "ro_mode"             => true,
          "login"               => "svenfuchs",
          "name"                => "Sven Fuchs"}},
        {"@type"                => "job",
        "@href"                 => "/v3/job/#{jobs[3].id}",
        "@representation"       => "standard",
        "@permissions"          => {
          "read"                => true,
          "cancel"              => true,
          "restart"             => true,
          "debug"               => false,
          "delete_log"          => false,
          "view_log"            => true,
          "prioritize"          => false },
        "id"                    => jobs[3].id,
        "private"          => false,
        "restarted_at"          => nil,
        "restarted_by"          => nil,
        "number"                => "#{jobs[3].number}",
        "state"                 => "configured",
        "started_at"            => "2010-11-12T13:00:00Z",
        "finished_at"           => nil,
        "allow_failure"         => jobs[3].allow_failure,
        "created_at"            => json_format_time_with_ms(jobs[3].created_at),
        "updated_at"            => json_format_time_with_ms(jobs[3].updated_at),
        "vm_size"               => nil,
        "config"                => {
          "rvm"                 => "1.9.2",
          "gemfile"             => "test/Gemfile.rails-3.0.x",
          "language"            => "ruby",
          "group"               => "stable",
          "dist"                => "precise",
          "os"                  => "linux" },
        "build"                 => {
          "@type"               => "build",
          "@href"               => "/v3/build/#{build.id}",
          "@representation"     => "minimal",
          "id"                  => build.id,
          "private"        => false,
          "priority"            => false,
          "number"              => build.number,
          "state"               => "configured",
          "duration"            => nil,
          "event_type"          => "push",
          "previous_state"      => "passed",
          "pull_request_number" => build.pull_request_number,
          "pull_request_title"  => build.pull_request_title,
          "started_at"          => "2010-11-12T13:00:00Z",
          "finished_at"         => nil},
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[1].id,
          "number"              => 2,
          "name"                => "deploy",
          "state"               => stages[1].state,
          "started_at"          => stages[1].started_at,
          "finished_at"         => stages[1].finished_at},
        "queue"                 => "builds.linux",
        "repository"            => {
          "@type"               => "repository",
          "@href"               => "/v3/repo/1",
          "@representation"     => "minimal",
          "id"                  => repo.id,
          "name"                => "minimal",
          "slug"                => "svenfuchs/minimal"},
        "commit"                => {
          "@type"               => "commit",
          "@representation"     => "minimal",
          "id"                  => commit.id,
          "sha"                 => commit.commit,
          "ref"                 => commit.ref,
          "message"             => commit.message,
          "compare_url"         => commit.compare_url,
          "committed_at"        => "2010-11-12T12:55:00Z"},
        "owner"                 => {
          "@type"               => "user",
          "@href"               => "/v3/user/1",
          "@representation"     => "minimal",
          "ro_mode"             => true,
          "id"                  => 1,
          "vcs_type"            => 'GithubUser',
          "login"               => "svenfuchs",
          "name"                => "Sven Fuchs"}}
        ]
      })
    }
  end

describe "jobs private repository, private API, authenticated as user with push access" do
    let(:token)   { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
    let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}"                        }}
    before        { Travis::API::V3::Models::Permission.create(repository: repo, user: repo.owner, pull: true, push: true) }
    before        { allow_any_instance_of(Travis::API::V3::Permissions::Job).to receive(:delete_log?).and_return(true) }
    before        { allow_any_instance_of(Travis::API::V3::Permissions::Job).to receive(:debug?).and_return(true) }
    before        { allow_any_instance_of(Travis::API::V3::Permissions::Job).to receive(:prioritize?).and_return(true) }
    before        { repo.update_attribute(:private, true)                             }
    before        { get("/v3/build/#{build.id}/jobs?include=job.config", {}, headers)                           }
    after         { repo.update_attribute(:private, false)                            }
    example       { expect(last_response).to be_ok                                    }
    example    { expect(parsed_body).to eql_json({
      "@type"              => "jobs",
      "@href"              => "/v3/build/#{build.id}/jobs?include=job.config",
      "@representation"    => "standard",
      "jobs"             => [{
        "@type"            => "job",
        "@href"            => "/v3/job/#{jobs[0].id}",
        "@representation"  => "standard",
        "@permissions"     => {
          "read"           => true,
          "cancel"         => true,
          "restart"        => true,
          "debug"          => true,
          "delete_log"     => true,
          "view_log"       => true,
          "prioritize"     => true },
        "id"               => jobs[0].id,
        "private"          => false,
        "restarted_at"     => nil,
        "restarted_by"     => nil,
        "number"           => "#{jobs[0].number}",
        "state"            => "configured",
        "started_at"       => "2010-11-12T13:00:00Z",
        "finished_at"      => nil,
        "allow_failure"    => jobs[0].allow_failure,
        "created_at"       => json_format_time_with_ms(jobs[0].created_at),
        "updated_at"       => json_format_time_with_ms(jobs[0].updated_at),
        "vm_size"          => nil,
        "config"           => {
          "rvm"            => "1.8.7",
          "gemfile"        => "test/Gemfile.rails-2.3.x",
          "language"       => "ruby",
          "group"          => "stable",
          "dist"           => "precise",
          "os"             => "linux" },
        "build"            => {
          "@type"          => "build",
          "@href"          => "/v3/build/#{build.id}",
          "@representation"=> "minimal",
          "id"             => build.id,
          "private"        => false,
          "priority"       => false,
          "number"         => build.number,
          "state"          => "configured",
          "duration"       => nil,
          "event_type"     => "push",
          "previous_state" => "passed",
          "pull_request_number" => build.pull_request_number,
          "pull_request_title"  => build.pull_request_title,
          "started_at"     => "2010-11-12T13:00:00Z",
          "finished_at"    => nil},
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[0].id,
          "number"              => 1,
          "name"                => "test",
          "state"               => stages[0].state,
          "started_at"          => stages[0].started_at,
          "finished_at"         => stages[0].finished_at},
        "queue"            => "builds.linux",
        "repository"       =>{
          "@type"          => "repository",
          "@href"          => "/v3/repo/1",
          "@representation"=>"minimal",
          "id"             => repo.id,
          "name"           => "minimal",
          "slug"           => "svenfuchs/minimal"},
        "commit"           =>{
          "@type"          => "commit",
          "@representation"=> "minimal",
          "id"             => commit.id,
          "sha"            => commit.commit,
          "ref"            => commit.ref,
          "message"        => commit.message,
          "compare_url"    => commit.compare_url,
          "committed_at"   =>"2010-11-12T12:55:00Z"},
        "owner"            =>{
          "@type"          => "user",
          "@href"          => "/v3/user/1",
          "@representation"=> "minimal",
          "id"             =>  1,
          "vcs_type"       => 'GithubUser',
          "ro_mode"        => true,
          "login"          => "svenfuchs",
          "name"           => "Sven Fuchs"}},
        {"@type"            => "job",
        "@href"            => "/v3/job/#{jobs[1].id}",
        "@representation"  => "standard",
        "@permissions"     => {
          "read"           => true,
          "cancel"         => true,
          "restart"        => true,
          "debug"          => true,
          "delete_log"     => true,
          "view_log"       => true,
          "prioritize"     => true },
        "id"               => jobs[1].id,
        "private"          => false,
        "restarted_at"     => nil,
        "restarted_by"     => nil,
        "number"           => "#{jobs[1].number}",
        "state"            => "configured",
        "started_at"       => "2010-11-12T13:00:00Z",
        "finished_at"      => nil,
        "allow_failure"    => jobs[1].allow_failure,
        "created_at"       => json_format_time_with_ms(jobs[1].created_at),
        "updated_at"       => json_format_time_with_ms(jobs[1].updated_at),
        "vm_size"          => nil,
        "config"           => {
          "rvm"            => "1.8.7",
          "gemfile"        => "test/Gemfile.rails-3.0.x",
          "language"       => "ruby",
          "group"          => "stable",
          "dist"           => "precise",
          "os"             => "linux" },
        "build"            => {
          "@type"          => "build",
          "@href"          => "/v3/build/#{build.id}",
          "@representation"=> "minimal",
          "id"             => build.id,
          "private"        => false,
          "priority"       => false,
          "number"         => build.number,
          "state"          => "configured",
          "duration"       => nil,
          "event_type"     => "push",
          "previous_state" => "passed",
          "pull_request_number" => build.pull_request_number,
          "pull_request_title"  => build.pull_request_title,
          "started_at"     => "2010-11-12T13:00:00Z",
          "finished_at"    => nil},
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[0].id,
          "number"              => 1,
          "name"                => "test",
          "state"               => stages[0].state,
          "started_at"          => stages[0].started_at,
          "finished_at"         => stages[0].finished_at},
        "queue"            => "builds.linux",
        "repository"       =>{
          "@type"          => "repository",
          "@href"          => "/v3/repo/1",
          "@representation"=>"minimal",
          "id"             => repo.id,
          "name"           => "minimal",
          "slug"           => "svenfuchs/minimal"},
        "commit"           =>{
          "@type"          => "commit",
          "@representation"=> "minimal",
          "id"             => commit.id,
          "sha"            => commit.commit,
          "ref"            => commit.ref,
          "message"        => commit.message,
          "compare_url"    => commit.compare_url,
          "committed_at"   =>"2010-11-12T12:55:00Z"},
        "owner"            =>{
          "@type"          => "user",
          "@href"          => "/v3/user/1",
          "@representation"=> "minimal",
          "id"             =>  1,
          "vcs_type"       => 'GithubUser',
          "ro_mode"        => true,
          "login"          => "svenfuchs",
          "name"           => "Sven Fuchs"}},
        {"@type"            => "job",
        "@href"            => "/v3/job/#{jobs[2].id}",
        "@representation"  => "standard",
        "@permissions"     => {
          "read"           => true,
          "cancel"         => true,
          "restart"        => true,
          "debug"          => true,
          "delete_log"     => true,
          "view_log"       => true,
          "prioritize"     => true },
        "id"               => jobs[2].id,
        "private"          => false,
        "restarted_at"     => nil,
        "restarted_by"     => nil,
        "number"           => "#{jobs[2].number}",
        "state"            => "configured",
        "started_at"       => "2010-11-12T13:00:00Z",
        "finished_at"      => nil,
        "allow_failure"    => jobs[2].allow_failure,
        "created_at"       => json_format_time_with_ms(jobs[2].created_at),
        "updated_at"       => json_format_time_with_ms(jobs[2].updated_at),
        "vm_size"          => nil,
        "config"           => {
          "rvm"            => "1.9.2",
          "gemfile"        => "test/Gemfile.rails-2.3.x",
          "language"       => "ruby",
          "group"          => "stable",
          "dist"           => "precise",
          "os"             => "linux" },
        "build"            => {
          "@type"          => "build",
          "@href"          => "/v3/build/#{build.id}",
          "@representation"=> "minimal",
          "id"             => build.id,
          "private"        => false,
          "priority"       => false,
          "number"         => build.number,
          "state"          => "configured",
          "duration"       => nil,
          "event_type"     => "push",
          "previous_state" => "passed",
          "pull_request_number" => build.pull_request_number,
          "pull_request_title"  => build.pull_request_title,
          "started_at"     => "2010-11-12T13:00:00Z",
          "finished_at"    => nil},
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[1].id,
          "number"              => 2,
          "name"                => "deploy",
          "state"               => stages[1].state,
          "started_at"          => stages[1].started_at,
          "finished_at"         => stages[1].finished_at},
        "queue"            => "builds.linux",
        "repository"       =>{
          "@type"          => "repository",
          "@href"          => "/v3/repo/1",
          "@representation"=>"minimal",
          "id"             => repo.id,
          "name"           => "minimal",
          "slug"           => "svenfuchs/minimal"},
        "commit"           =>{
          "@type"          => "commit",
          "@representation"=> "minimal",
          "id"             => commit.id,
          "sha"            => commit.commit,
          "ref"            => commit.ref,
          "message"        => commit.message,
          "compare_url"    => commit.compare_url,
          "committed_at"   =>"2010-11-12T12:55:00Z"},
        "owner"            =>{
          "@type"          => "user",
          "@href"          => "/v3/user/1",
          "@representation"=> "minimal",
          "id"             =>  1,
          "vcs_type"       => 'GithubUser',
          "ro_mode"        => true,
          "login"          => "svenfuchs",
          "name"           => "Sven Fuchs"}},
        {"@type"            => "job",
        "@href"            => "/v3/job/#{jobs[3].id}",
        "@representation"  => "standard",
        "@permissions"     => {
          "read"           => true,
          "cancel"         => true,
          "restart"        => true,
          "debug"          => true,
          "delete_log"     => true,
          "view_log"       => true,
          "prioritize"     => true },
        "id"               => jobs[3].id,
        "private"          => false,
        "restarted_at"     => nil,
        "restarted_by"     => nil,
        "number"           => "#{jobs[3].number}",
        "state"            => "configured",
        "started_at"       => "2010-11-12T13:00:00Z",
        "finished_at"      => nil,
        "allow_failure"    => jobs[3].allow_failure,
        "created_at"       => json_format_time_with_ms(jobs[3].created_at),
        "updated_at"       => json_format_time_with_ms(jobs[3].updated_at),
        "vm_size"          => nil,
        "config"           => {
          "rvm"            => "1.9.2",
          "gemfile"        => "test/Gemfile.rails-3.0.x",
          "language"       => "ruby",
          "group"          => "stable",
          "dist"           => "precise",
          "os"             => "linux" },
        "build"            => {
          "@type"          => "build",
          "@href"          => "/v3/build/#{build.id}",
          "@representation"=> "minimal",
          "id"             => build.id,
          "private"        => false,
          "priority"       => false,
          "number"         => build.number,
          "state"          => "configured",
          "duration"       => nil,
          "event_type"     => "push",
          "previous_state" => "passed",
          "pull_request_number" => nil,
          "pull_request_title" => nil,
          "started_at"     => "2010-11-12T13:00:00Z",
          "finished_at"    => nil},
        "stage"                 => {
          "@type"               => "stage",
          "@representation"     => "minimal",
          "id"                  => stages[1].id,
          "number"              => 2,
          "name"                => "deploy",
          "state"               => stages[1].state,
          "started_at"          => stages[1].started_at,
          "finished_at"         => stages[1].finished_at},
        "queue"            => "builds.linux",
        "repository"       =>{
          "@type"          => "repository",
          "@href"          => "/v3/repo/1",
          "@representation"=>"minimal",
          "id"             => repo.id,
          "name"           => "minimal",
          "slug"           => "svenfuchs/minimal"},
        "commit"           =>{
          "@type"          => "commit",
          "@representation"=> "minimal",
          "id"             => commit.id,
          "sha"            => commit.commit,
          "ref"            => commit.ref,
          "message"        => commit.message,
          "compare_url"    => commit.compare_url,
          "committed_at"   =>"2010-11-12T12:55:00Z"},
        "owner"            =>{
          "@type"          => "user",
          "@href"          => "/v3/user/1",
          "@representation"=> "minimal",
          "id"             =>  1,
          "vcs_type"       => 'GithubUser',
          "ro_mode"        => true,
          "login"          => "svenfuchs",
          "name"           => "Sven Fuchs"}}
        ]
      })
    }
  end
end
