File: user_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (133 lines) | stat: -rw-r--r-- 3,623 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require "spec"
require "system/user"

{% if flag?(:win32) %}
  {% name, id = `whoami /USER /FO TABLE /NH`.stringify.chomp.split(" ") %}
  USER_NAME = {{ name }}
  USER_ID   = {{ id }}
{% else %}
  USER_NAME = {{ `id -un`.stringify.chomp }}
  USER_ID   = {{ `id -u`.stringify.chomp }}
{% end %}

INVALID_USER_NAME = "this_user_does_not_exist"
INVALID_USER_ID   = {% if flag?(:android) %}"8888"{% else %}"1234567"{% end %}

def normalized_username(username)
  # on Windows, domain names are case-insensitive, so we unify the letter case
  # from sources like `whoami`, `hostname`, or Win32 APIs
  {% if flag?(:win32) %}
    domain, _, user = username.partition('\\')
    "#{domain.upcase}\\#{user}"
  {% else %}
    username
  {% end %}
end

describe System::User do
  describe ".find_by(*, name)" do
    it "returns a user by name" do
      user = System::User.find_by(name: USER_NAME)

      user.should be_a(System::User)
      normalized_username(user.username).should eq(normalized_username(USER_NAME))
      user.id.should eq(USER_ID)
    end

    it "raises on a nonexistent user" do
      expect_raises System::User::NotFoundError, "No such user" do
        System::User.find_by(name: INVALID_USER_NAME)
      end
    end
  end

  describe ".find_by(*, id)" do
    it "returns a user by id" do
      user = System::User.find_by(id: USER_ID)

      user.should be_a(System::User)
      user.id.should eq(USER_ID)
      normalized_username(user.username).should eq(normalized_username(USER_NAME))
    end

    it "raises on nonexistent user id" do
      expect_raises System::User::NotFoundError, "No such user" do
        System::User.find_by(id: INVALID_USER_ID)
      end
    end
  end

  describe ".find_by?(*, name)" do
    it "returns a user by name" do
      user = System::User.find_by?(name: USER_NAME).not_nil!

      user.should be_a(System::User)
      normalized_username(user.username).should eq(normalized_username(USER_NAME))
      user.id.should eq(USER_ID)
    end

    it "returns nil on nonexistent user" do
      user = System::User.find_by?(name: INVALID_USER_NAME)
      user.should eq(nil)
    end
  end

  describe ".find_by?(*, id)" do
    it "returns a user by id" do
      user = System::User.find_by?(id: USER_ID).not_nil!

      user.should be_a(System::User)
      user.id.should eq(USER_ID)
      normalized_username(user.username).should eq(normalized_username(USER_NAME))
    end

    it "returns nil on nonexistent user id" do
      user = System::User.find_by?(id: INVALID_USER_ID)
      user.should eq(nil)
    end
  end

  describe "#username" do
    it "is the same as the source name" do
      user = System::User.find_by(name: USER_NAME)
      normalized_username(user.username).should eq(normalized_username(USER_NAME))
    end
  end

  describe "#id" do
    it "is the same as the source ID" do
      System::User.find_by(id: USER_ID).id.should eq(USER_ID)
    end
  end

  describe "#group_id" do
    it "calls without raising" do
      System::User.find_by(name: USER_NAME).group_id
    end
  end

  describe "#name" do
    it "calls without raising" do
      System::User.find_by(name: USER_NAME).name
    end
  end

  describe "#home_directory" do
    it "calls without raising" do
      System::User.find_by(name: USER_NAME).home_directory
    end
  end

  describe "#shell" do
    it "calls without raising" do
      System::User.find_by(name: USER_NAME).shell
    end
  end

  describe "#to_s" do
    it "returns a string representation" do
      user = System::User.find_by(name: USER_NAME)
      user.to_s.should eq("#{user.username} (#{user.id})")
    end
  end
end