users_spec.rb 3.2 KB
Newer Older
N
Nihad Abbasov 已提交
1 2 3
require 'spec_helper'

describe Gitlab::API do
4 5
  include ApiHelpers

6
  let(:user)  { Factory :user }
7
  let(:admin) { Factory :admin }
8
  let(:key)   { Factory :key, user: user }
N
Nihad Abbasov 已提交
9 10

  describe "GET /users" do
11 12 13 14 15
    context "when unauthenticated" do
      it "should return authentication error" do
        get api("/users")
        response.status.should == 401
      end
N
Nihad Abbasov 已提交
16 17
    end

18
    context "when authenticated" do
N
Nihad Abbasov 已提交
19
      it "should return an array of users" do
R
Robert Speicher 已提交
20
        get api("/users", user)
N
Nihad Abbasov 已提交
21
        response.status.should == 200
N
Nihad Abbasov 已提交
22 23
        json_response.should be_an Array
        json_response.first['email'].should == user.email
N
Nihad Abbasov 已提交
24 25 26 27 28 29
      end
    end
  end

  describe "GET /users/:id" do
    it "should return a user by id" do
R
Robert Speicher 已提交
30
      get api("/users/#{user.id}", user)
N
Nihad Abbasov 已提交
31
      response.status.should == 200
N
Nihad Abbasov 已提交
32
      json_response['email'].should == user.email
N
Nihad Abbasov 已提交
33 34 35
    end
  end

V
Valeriy Sizov 已提交
36 37 38 39 40 41 42 43 44
  describe "POST /users" do
    before{ admin }

    it "should not create invalid user" do
      post api("/users", admin), { email: "invalid email" }
      response.status.should == 404
    end

    it "should create user" do
45 46 47
      expect {
        post api("/users", admin), Factory.attributes(:user, projects_limit: 3)
      }.to change { User.count }.by(1)
V
Valeriy Sizov 已提交
48 49 50 51 52 53 54 55
    end

    it "shouldn't available for non admin users" do
      post api("/users", user), Factory.attributes(:user)
      response.status.should == 403
    end
  end

N
Nihad Abbasov 已提交
56 57
  describe "GET /user" do
    it "should return current user" do
R
Robert Speicher 已提交
58
      get api("/user", user)
N
Nihad Abbasov 已提交
59
      response.status.should == 200
N
Nihad Abbasov 已提交
60
      json_response['email'].should == user.email
N
Nihad Abbasov 已提交
61 62
    end
  end
63 64 65 66 67 68 69 70

  describe "GET /user/keys" do
    context "when unauthenticated" do
      it "should return authentication error" do
        get api("/user/keys")
        response.status.should == 401
      end
    end
N
Nihad Abbasov 已提交
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    context "when authenticated" do
      it "should return array of ssh keys" do
        user.keys << key
        user.save
        get api("/user/keys", user)
        response.status.should == 200
        json_response.should be_an Array
        json_response.first["title"].should == key.title
      end
    end
  end

  describe "GET /user/keys/:id" do
    it "should returm single key" do
      user.keys << key
      user.save
      get api("/user/keys/#{key.id}", user)
      response.status.should == 200
      json_response["title"].should == key.title
    end
N
Nihad Abbasov 已提交
92

93 94 95 96 97 98 99 100 101 102 103
    it "should return 404 Not Found within invalid ID" do
      get api("/user/keys/42", user)
      response.status.should == 404
    end
  end

  describe "POST /user/keys" do
    it "should not create invalid ssh key" do
      post api("/user/keys", user), { title: "invalid key" }
      response.status.should == 404
    end
N
Nihad Abbasov 已提交
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    it "should create ssh key" do
      key_attrs = Factory.attributes :key
      expect {
        post api("/user/keys", user), key_attrs
      }.to change{ user.keys.count }.by(1)
    end
  end

  describe "DELETE /user/keys/:id" do
    it "should delete existed key" do
      user.keys << key
      user.save
      expect {
        delete api("/user/keys/#{key.id}", user)
      }.to change{user.keys.count}.by(-1)
    end
N
Nihad Abbasov 已提交
121

122 123 124 125 126
    it "should return 404 Not Found within invalid ID" do
      delete api("/user/keys/42", user)
      response.status.should == 404
    end
  end
N
Nihad Abbasov 已提交
127
end