projects_spec.rb 4.55 KB
Newer Older
Nihad Abbasov's avatar
Nihad Abbasov committed
1 2 3
require 'spec_helper'

describe Gitlab::API do
4 5
  include ApiHelpers

Nihad Abbasov's avatar
Nihad Abbasov committed
6
  let(:user) { Factory :user }
7 8
  let!(:project) { Factory :project, owner: user }
  let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' }
9
  before { project.add_access(user, :read) }
Nihad Abbasov's avatar
Nihad Abbasov committed
10 11 12

  describe "GET /projects" do
    it "should return authentication error" do
13
      get api("/projects")
Nihad Abbasov's avatar
Nihad Abbasov committed
14 15 16 17 18
      response.status.should == 401
    end

    describe "authenticated GET /projects" do
      it "should return an array of projects" do
19
        get api("/projects", user)
Nihad Abbasov's avatar
Nihad Abbasov committed
20
        response.status.should == 200
Nihad Abbasov's avatar
Nihad Abbasov committed
21 22 23
        json_response.should be_an Array
        json_response.first['name'].should == project.name
        json_response.first['owner']['email'].should == user.email
Nihad Abbasov's avatar
Nihad Abbasov committed
24 25 26 27 28 29
      end
    end
  end

  describe "GET /projects/:id" do
    it "should return a project by id" do
30
      get api("/projects/#{project.id}", user)
Nihad Abbasov's avatar
Nihad Abbasov committed
31
      response.status.should == 200
Nihad Abbasov's avatar
Nihad Abbasov committed
32 33
      json_response['name'].should == project.name
      json_response['owner']['email'].should == user.email
Nihad Abbasov's avatar
Nihad Abbasov committed
34
    end
35 36

    it "should return a project by code name" do
37
      get api("/projects/#{project.code}", user)
38 39 40
      response.status.should == 200
      json_response['name'].should == project.name
    end
41 42

    it "should return a 404 error if not found" do
43
      get api("/projects/42", user)
44 45 46
      response.status.should == 404
      json_response['message'].should == '404 Not found'
    end
Nihad Abbasov's avatar
Nihad Abbasov committed
47 48 49 50
  end

  describe "GET /projects/:id/repository/branches" do
    it "should return an array of project branches" do
51
      get api("/projects/#{project.code}/repository/branches", user)
Nihad Abbasov's avatar
Nihad Abbasov committed
52
      response.status.should == 200
Nihad Abbasov's avatar
Nihad Abbasov committed
53 54
      json_response.should be_an Array
      json_response.first['name'].should == project.repo.heads.sort_by(&:name).first.name
Nihad Abbasov's avatar
Nihad Abbasov committed
55 56 57
    end
  end

58 59
  describe "GET /projects/:id/repository/branches/:branch" do
    it "should return the branch information for a single branch" do
60
      get api("/projects/#{project.code}/repository/branches/new_design", user)
61 62 63 64 65 66 67
      response.status.should == 200

      json_response['name'].should == 'new_design'
      json_response['commit']['id'].should == '621491c677087aa243f165eab467bfdfbee00be1'
    end
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
68 69
  describe "GET /projects/:id/repository/tags" do
    it "should return an array of project tags" do
70
      get api("/projects/#{project.code}/repository/tags", user)
Nihad Abbasov's avatar
Nihad Abbasov committed
71
      response.status.should == 200
Nihad Abbasov's avatar
Nihad Abbasov committed
72 73
      json_response.should be_an Array
      json_response.first['name'].should == project.repo.tags.sort_by(&:name).reverse.first.name
Nihad Abbasov's avatar
Nihad Abbasov committed
74 75
    end
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
76 77 78

  describe "GET /projects/:id/snippets/:snippet_id" do
    it "should return a project snippet" do
79
      get api("/projects/#{project.code}/snippets/#{snippet.id}", user)
Nihad Abbasov's avatar
Nihad Abbasov committed
80
      response.status.should == 200
Nihad Abbasov's avatar
Nihad Abbasov committed
81
      json_response['title'].should == snippet.title
Nihad Abbasov's avatar
Nihad Abbasov committed
82 83 84 85 86
    end
  end

  describe "POST /projects/:id/snippets" do
    it "should create a new project snippet" do
87
      post api("/projects/#{project.code}/snippets", user),
88
        title: 'api test', file_name: 'sample.rb', code: 'test'
Nihad Abbasov's avatar
Nihad Abbasov committed
89
      response.status.should == 201
Nihad Abbasov's avatar
Nihad Abbasov committed
90
      json_response['title'].should == 'api test'
Nihad Abbasov's avatar
Nihad Abbasov committed
91 92 93
    end
  end

94 95
  describe "PUT /projects/:id/snippets" do
    it "should update an existing project snippet" do
96
      put api("/projects/#{project.code}/snippets/#{snippet.id}", user),
97
        code: 'updated code'
98 99 100 101 102 103
      response.status.should == 200
      json_response['title'].should == 'example'
      snippet.reload.content.should == 'updated code'
    end
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
104
  describe "DELETE /projects/:id/snippets/:snippet_id" do
m16a1's avatar
m16a1 committed
105
    it "should delete existing project snippet" do
Nihad Abbasov's avatar
Nihad Abbasov committed
106
      expect {
107
        delete api("/projects/#{project.code}/snippets/#{snippet.id}", user)
108
      }.to change { Snippet.count }.by(-1)
Nihad Abbasov's avatar
Nihad Abbasov committed
109 110
    end
  end
111 112 113

  describe "GET /projects/:id/snippets/:snippet_id/raw" do
    it "should get a raw project snippet" do
114
      get api("/projects/#{project.code}/snippets/#{snippet.id}/raw", user)
115 116 117
      response.status.should == 200
    end
  end
118 119 120

  describe "GET /projects/:id/:sha/blob" do
    it "should get the raw file contents" do
121
      get api("/projects/#{project.code}/repository/commits/master/blob?filepath=README.md", user)
122 123 124 125
      response.status.should == 200
    end

    it "should return 404 for invalid branch_name" do
126
      get api("/projects/#{project.code}/repository/commits/invalid_branch_name/blob?filepath=README.md", user)
127 128 129 130
      response.status.should == 404
    end

    it "should return 404 for invalid file" do
131
      get api("/projects/#{project.code}/repository/commits/master/blob?filepath=README.invalid", user)
132 133 134
      response.status.should == 404
    end
  end
Nihad Abbasov's avatar
Nihad Abbasov committed
135
end