-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_auth_generator.cr
More file actions
212 lines (171 loc) · 6.79 KB
/
test_auth_generator.cr
File metadata and controls
212 lines (171 loc) · 6.79 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env crystal
# Auth Generator Test Script
require "process"
require "file_utils"
require "json"
puts "🔐 Testing Auth Generator with HTTP POST requests"
# Create test directory
test_dir = "/tmp/auth_test"
project_name = "authtest"
puts "📁 Creating test directory: #{test_dir}"
FileUtils.rm_rf(test_dir) if Dir.exists?(test_dir)
Dir.mkdir_p(test_dir)
Dir.cd(test_dir)
puts "🚀 Creating new Azu project: #{project_name}"
result = Process.run("/Users/eperez/Workspaces/azu_cli/bin/azu new #{project_name} --type=web --author=\"Test Author\" --email=\"test@example.com\" --no-git --no-example", shell: true, output: Process::Redirect::Pipe, error: Process::Redirect::Pipe)
unless result.success?
puts "❌ Failed to create project: #{result.error_output}"
exit 1
end
puts "📂 Project created, entering project directory..."
Dir.cd(project_name)
puts "🔨 Building project..."
result = Process.run("shards build", shell: true, output: Process::Redirect::Pipe, error: Process::Redirect::Pipe)
if result.success?
puts "✅ Project builds successfully!"
else
puts "❌ Project build failed: #{result.error_output}"
exit 1
end
puts "🔐 Generating Auth system..."
result = Process.run("/Users/eperez/Workspaces/azu_cli/bin/azu generate auth", shell: true, output: Process::Redirect::Pipe, error: Process::Redirect::Pipe)
if result.success?
puts "✅ Auth system generated successfully!"
puts "Auth generation output: #{result.output}"
else
puts "❌ Auth generation failed: #{result.error_output}"
exit 1
end
puts "📋 Checking auth files created..."
auth_files = [
"src/models/user.cr",
"src/endpoints/auth_endpoint.cr",
"src/requests/auth/login_request.cr",
"src/requests/auth/register_request.cr",
"src/config/authly.cr",
]
auth_files.each do |file|
if File.exists?(file)
puts "✅ #{file}"
else
puts "❌ #{file} - MISSING"
end
end
puts "🔨 Rebuilding project with auth..."
result = Process.run("shards build", shell: true, output: Process::Redirect::Pipe, error: Process::Redirect::Pipe)
if result.success?
puts "✅ Project with auth builds successfully!"
else
puts "❌ Project with auth build failed: #{result.error_output}"
puts "Build error details: #{result.error_output}"
exit 1
end
puts "🌐 Starting server in background..."
# Start server in background
server_process = Process.new("shards", ["run", "src/server.cr"], output: Process::Redirect::Pipe, error: Process::Redirect::Pipe)
# Wait for server to start
puts "⏳ Waiting for server to start..."
sleep 8
# Test auth endpoints with HTTP POST requests
puts "🌍 Testing Auth endpoints with HTTP POST requests..."
# Test register endpoint
puts "\n📝 Testing POST /auth/register..."
register_data = {
"email" => "test@example.com",
"password" => "password123",
"password_confirmation" => "password123",
}.to_json
result = Process.run("curl -s -X POST -H 'Content-Type: application/json' -d '#{register_data}' http://localhost:3000/auth/register", shell: true, output: Process::Redirect::Pipe, error: Process::Redirect::Pipe)
puts "Register response status: #{result.exit_code}"
puts "Register response body: #{result.output}"
if result.exit_code == 0
puts "✅ Register endpoint responded"
else
puts "❌ Register endpoint failed"
end
# Test login endpoint
puts "\n🔑 Testing POST /auth/login..."
login_data = {
"email" => "test@example.com",
"password" => "password123",
}.to_json
result = Process.run("curl -s -X POST -H 'Content-Type: application/json' -d '#{login_data}' http://localhost:3000/auth/login", shell: true, output: Process::Redirect::Pipe, error: Process::Redirect::Pipe)
puts "Login response status: #{result.exit_code}"
puts "Login response body: #{result.output}"
if result.exit_code == 0
puts "✅ Login endpoint responded"
else
puts "❌ Login endpoint failed"
end
# Test refresh token endpoint
puts "\n🔄 Testing POST /auth/refresh..."
refresh_data = {
"refresh_token" => "dummy_refresh_token",
}.to_json
result = Process.run("curl -s -X POST -H 'Content-Type: application/json' -d '#{refresh_data}' http://localhost:3000/auth/refresh", shell: true, output: Process::Redirect::Pipe, error: Process::Redirect::Pipe)
puts "Refresh response status: #{result.exit_code}"
puts "Refresh response body: #{result.output}"
if result.exit_code == 0
puts "✅ Refresh endpoint responded"
else
puts "❌ Refresh endpoint failed"
end
# Test change password endpoint
puts "\n🔒 Testing POST /auth/change_password..."
change_password_data = {
"current_password" => "password123",
"new_password" => "newpassword123",
"new_password_confirmation" => "newpassword123",
}.to_json
result = Process.run("curl -s -X POST -H 'Content-Type: application/json' -d '#{change_password_data}' http://localhost:3000/auth/change_password", shell: true, output: Process::Redirect::Pipe, error: Process::Redirect::Pipe)
puts "Change password response status: #{result.exit_code}"
puts "Change password response body: #{result.output}"
if result.exit_code == 0
puts "✅ Change password endpoint responded"
else
puts "❌ Change password endpoint failed"
end
# Test logout endpoint
puts "\n🚪 Testing POST /auth/logout..."
logout_data = {
"refresh_token" => "dummy_refresh_token",
}.to_json
result = Process.run("curl -s -X POST -H 'Content-Type: application/json' -d '#{logout_data}' http://localhost:3000/auth/logout", shell: true, output: Process::Redirect::Pipe, error: Process::Redirect::Pipe)
puts "Logout response status: #{result.exit_code}"
puts "Logout response body: #{result.output}"
if result.exit_code == 0
puts "✅ Logout endpoint responded"
else
puts "❌ Logout endpoint failed"
end
# Test basic endpoints still work
puts "\n🌐 Testing basic endpoints still work..."
# Test root endpoint
result = Process.run("curl -s -o /dev/null -w \"%{http_code}\" http://localhost:3000/", shell: true, output: Process::Redirect::Pipe)
if result.output.to_s.strip == "200"
puts "✅ Root endpoint still works"
else
puts "❌ Root endpoint broken: #{result.output}"
end
# Test welcome endpoint
result = Process.run("curl -s -o /dev/null -w \"%{http_code}\" http://localhost:3000/welcome", shell: true, output: Process::Redirect::Pipe)
if result.output.to_s.strip == "200"
puts "✅ Welcome endpoint still works"
else
puts "❌ Welcome endpoint broken: #{result.output}"
end
# Stop server
puts "\n🛑 Stopping server..."
server_process.terminate
server_process.wait
puts "\n🧹 Cleaning up..."
Dir.cd("..")
FileUtils.rm_rf(test_dir)
puts "\n✅ Auth generator test completed!"
puts "📊 Summary:"
puts " - Project created and built successfully"
puts " - Auth system generated"
puts " - All auth files created"
puts " - Project rebuilt with auth successfully"
puts " - Server started and responded to HTTP requests"
puts " - All auth endpoints tested with POST requests"