Skip to content

Commit

Permalink
Put auth tests together
Browse files Browse the repository at this point in the history
Followup to #171 (comment)

This puts the caching_sha2 and native plugin tests together with the
cleartext ones.
  • Loading branch information
composerinteralia committed Apr 8, 2024
1 parent 14e7dbc commit 445f94f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 81 deletions.
81 changes: 81 additions & 0 deletions contrib/ruby/test/auth_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,87 @@
require "test_helper"

class AuthTest < TrilogyTest
def has_caching_sha2?
server_version = new_tcp_client.server_version
server_version.split(".", 2)[0].to_i >= 8
end

def test_connect_native_password
client = new_tcp_client username: "native"

refute_nil client
ensure
ensure_closed client
end

def test_connect_caching_sha2
return skip unless has_caching_sha2?

# Ensure correct setup
assert_equal [["caching_sha2_password"]], new_tcp_client.query("SELECT plugin FROM mysql.user WHERE user = 'caching_sha2'").rows

client = new_tcp_client username: "caching_sha2", password: "password"

refute_nil client
ensure
ensure_closed client
end

def test_connect_with_unix_and_caching_sha2_works
return skip unless has_caching_sha2?
return skip unless ["127.0.0.1", "localhost"].include?(DEFAULT_HOST)

socket = new_tcp_client.query("SHOW VARIABLES LIKE 'socket'").to_a[0][1]

if !File.exist?(socket)
skip "cound not find socket at #{socket}"
end

client = new_unix_client(socket, username: "caching_sha2", password: "password")
refute_nil client
ensure
ensure_closed client
end

def test_connect_without_ssl_or_unix_socket_caching_sha2_raises
return skip unless has_caching_sha2?

# Ensure correct setup
assert_equal [["caching_sha2_password"]], new_tcp_client.query("SELECT plugin FROM mysql.user WHERE user = 'caching_sha2'").rows

options = {
host: DEFAULT_HOST,
port: DEFAULT_PORT,
username: "caching_sha2",
password: "password",
ssl: false,
ssl_mode: 0
}

err = assert_raises Trilogy::ConnectionError do
new_tcp_client options
end

assert_includes err.message, "TRILOGY_UNSUPPORTED"
assert_includes err.message, "caching_sha2_password requires either TCP with TLS or a unix socket"
end

def test_connection_error_native
err = assert_raises Trilogy::ConnectionError do
new_tcp_client(username: "native", password: "incorrect")
end
assert_includes err.message, "Access denied for user 'native"
end

def test_connection_error_caching_sha2
return skip unless has_caching_sha2?

err = assert_raises Trilogy::ConnectionError do
new_tcp_client(username: "caching_sha2", password: "incorrect")
end
assert_includes err.message, "Access denied for user 'caching_sha2"
end

def test_cleartext_auth_plugin
client = new_tcp_client username: "cleartext_user", password: "password", enable_cleartext_plugin: true
refute_nil client
Expand Down
81 changes: 0 additions & 81 deletions contrib/ruby/test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,87 +23,6 @@ def test_trilogy_connect_tcp_string_host
end
end

def has_caching_sha2?
server_version = new_tcp_client.server_version
server_version.split(".", 2)[0].to_i >= 8
end

def test_connect_native_password
client = new_tcp_client username: "native"

refute_nil client
ensure
ensure_closed client
end

def test_connect_caching_sha2
return skip unless has_caching_sha2?

# Ensure correct setup
assert_equal [["caching_sha2_password"]], new_tcp_client.query("SELECT plugin FROM mysql.user WHERE user = 'caching_sha2'").rows

client = new_tcp_client username: "caching_sha2", password: "password"

refute_nil client
ensure
ensure_closed client
end

def test_connect_with_unix_and_caching_sha2_works
return skip unless has_caching_sha2?
return skip unless ["127.0.0.1", "localhost"].include?(DEFAULT_HOST)

socket = new_tcp_client.query("SHOW VARIABLES LIKE 'socket'").to_a[0][1]

if !File.exist?(socket)
skip "cound not find socket at #{socket}"
end

client = new_unix_client(socket, username: "caching_sha2", password: "password")
refute_nil client
ensure
ensure_closed client
end

def test_connect_without_ssl_or_unix_socket_caching_sha2_raises
return skip unless has_caching_sha2?

# Ensure correct setup
assert_equal [["caching_sha2_password"]], new_tcp_client.query("SELECT plugin FROM mysql.user WHERE user = 'caching_sha2'").rows

options = {
host: DEFAULT_HOST,
port: DEFAULT_PORT,
username: "caching_sha2",
password: "password",
ssl: false,
ssl_mode: 0
}

err = assert_raises Trilogy::ConnectionError do
new_tcp_client options
end

assert_includes err.message, "TRILOGY_UNSUPPORTED"
assert_includes err.message, "caching_sha2_password requires either TCP with TLS or a unix socket"
end

def test_connection_error_native
err = assert_raises Trilogy::ConnectionError do
new_tcp_client(username: "native", password: "incorrect")
end
assert_includes err.message, "Access denied for user 'native"
end

def test_connection_error_caching_sha2
return skip unless has_caching_sha2?

err = assert_raises Trilogy::ConnectionError do
new_tcp_client(username: "caching_sha2", password: "incorrect")
end
assert_includes err.message, "Access denied for user 'caching_sha2"
end

def test_trilogy_connect_tcp_to_wrong_port
e = assert_raises Trilogy::ConnectionError do
new_tcp_client port: 13307
Expand Down

0 comments on commit 445f94f

Please sign in to comment.