1+ require 'spec_helper'
2+ require_relative '../../libraries/ident'
3+
4+ RSpec . describe PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFile do
5+ let ( :ident_file ) { described_class . new }
6+
7+ describe '#add' do
8+ let ( :entry1 ) do
9+ PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFileEntry . new (
10+ map_name : 'testmap' ,
11+ system_username : 'user1' ,
12+ database_username : 'dbuser1'
13+ )
14+ end
15+
16+ let ( :entry2 ) do
17+ PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFileEntry . new (
18+ map_name : 'testmap' ,
19+ system_username : 'user2' ,
20+ database_username : 'dbuser2'
21+ )
22+ end
23+
24+ let ( :duplicate_entry ) do
25+ PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFileEntry . new (
26+ map_name : 'testmap' ,
27+ system_username : 'user1' ,
28+ database_username : 'dbuser1'
29+ )
30+ end
31+
32+ it 'allows multiple entries with the same map_name but different system/database usernames' do
33+ result1 = ident_file . add ( entry1 )
34+ result2 = ident_file . add ( entry2 )
35+
36+ expect ( result1 ) . to be_truthy
37+ expect ( result2 ) . to be_truthy
38+ expect ( ident_file . entries . size ) . to eq ( 2 )
39+ end
40+
41+ it 'prevents adding duplicate entries' do
42+ ident_file . add ( entry1 )
43+ result = ident_file . add ( duplicate_entry )
44+
45+ expect ( result ) . to be_falsy
46+ expect ( ident_file . entries . size ) . to eq ( 1 )
47+ end
48+ end
49+
50+ describe '#entry' do
51+ let ( :entry1 ) do
52+ PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFileEntry . new (
53+ map_name : 'testmap' ,
54+ system_username : 'user1' ,
55+ database_username : 'dbuser1'
56+ )
57+ end
58+
59+ let ( :entry2 ) do
60+ PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFileEntry . new (
61+ map_name : 'testmap' ,
62+ system_username : 'user2' ,
63+ database_username : 'dbuser2'
64+ )
65+ end
66+
67+ before do
68+ ident_file . add ( entry1 )
69+ ident_file . add ( entry2 )
70+ end
71+
72+ it 'returns specific entry when all parameters are provided' do
73+ result = ident_file . entry ( 'testmap' , 'user1' , 'dbuser1' )
74+ expect ( result ) . to eq ( entry1 )
75+
76+ result = ident_file . entry ( 'testmap' , 'user2' , 'dbuser2' )
77+ expect ( result ) . to eq ( entry2 )
78+ end
79+
80+ it 'returns first entry when only map_name is provided and multiple entries exist' do
81+ result = ident_file . entry ( 'testmap' )
82+ expect ( result ) . to be_a ( PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFileEntry )
83+ expect ( result . map_name ) . to eq ( 'testmap' )
84+ end
85+
86+ it 'returns nil when no matching entry is found' do
87+ result = ident_file . entry ( 'testmap' , 'nonexistent' , 'user' )
88+ expect ( result ) . to be_nil
89+ end
90+ end
91+
92+ describe '#include?' do
93+ let ( :entry ) do
94+ PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFileEntry . new (
95+ map_name : 'testmap' ,
96+ system_username : 'user1' ,
97+ database_username : 'dbuser1'
98+ )
99+ end
100+
101+ let ( :different_entry ) do
102+ PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFileEntry . new (
103+ map_name : 'testmap' ,
104+ system_username : 'user2' ,
105+ database_username : 'dbuser2'
106+ )
107+ end
108+
109+ it 'returns true for existing entries' do
110+ ident_file . add ( entry )
111+ expect ( ident_file . include? ( entry ) ) . to be_truthy
112+ end
113+
114+ it 'returns false for non-existing entries' do
115+ ident_file . add ( entry )
116+ expect ( ident_file . include? ( different_entry ) ) . to be_falsy
117+ end
118+ end
119+
120+ describe 'issue scenario' do
121+ # Test the specific scenario from GitHub issue #787
122+ it 'allows multiple mappings per map name as described in the issue' do
123+ entry1 = PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFileEntry . new (
124+ map_name : 'someuser_postgres' ,
125+ system_username : 'someuser' ,
126+ database_username : 'postgres'
127+ )
128+
129+ entry2 = PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFileEntry . new (
130+ map_name : 'someuser_postgres' ,
131+ system_username : 'postgres' ,
132+ database_username : 'postgres'
133+ )
134+
135+ result1 = ident_file . add ( entry1 )
136+ result2 = ident_file . add ( entry2 )
137+
138+ expect ( result1 ) . to be_truthy
139+ expect ( result2 ) . to be_truthy
140+ expect ( ident_file . entries . size ) . to eq ( 2 )
141+
142+ # Verify both entries can be retrieved
143+ retrieved1 = ident_file . entry ( 'someuser_postgres' , 'someuser' , 'postgres' )
144+ retrieved2 = ident_file . entry ( 'someuser_postgres' , 'postgres' , 'postgres' )
145+
146+ expect ( retrieved1 ) . to eq ( entry1 )
147+ expect ( retrieved2 ) . to eq ( entry2 )
148+ end
149+ end
150+ end
151+
152+ RSpec . describe PostgreSQL ::Cookbook ::IdentHelpers ::PgIdent ::PgIdentFileEntry do
153+ describe '#eql?' do
154+ let ( :entry1 ) do
155+ described_class . new (
156+ map_name : 'testmap' ,
157+ system_username : 'user1' ,
158+ database_username : 'dbuser1'
159+ )
160+ end
161+
162+ let ( :entry2 ) do
163+ described_class . new (
164+ map_name : 'testmap' ,
165+ system_username : 'user1' ,
166+ database_username : 'dbuser1'
167+ )
168+ end
169+
170+ let ( :entry3 ) do
171+ described_class . new (
172+ map_name : 'testmap' ,
173+ system_username : 'user2' ,
174+ database_username : 'dbuser1'
175+ )
176+ end
177+
178+ it 'returns true for entries with same map_name, system_username, and database_username' do
179+ expect ( entry1 . eql? ( entry2 ) ) . to be_truthy
180+ end
181+
182+ it 'returns false for entries with different system_username' do
183+ expect ( entry1 . eql? ( entry3 ) ) . to be_falsy
184+ end
185+ end
186+ end
0 commit comments