|
| 1 | +# Copyright 2020-2021 Couchbase, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +require_relative "test_helper" |
| 16 | + |
| 17 | +module Couchbase |
| 18 | + class UserManagerTest < Minitest::Test |
| 19 | + include TestUtilities |
| 20 | + |
| 21 | + def setup |
| 22 | + connect |
| 23 | + @test_username = "test_user" |
| 24 | + @test_password = "a_password" |
| 25 | + @test_user = Management::User.new do |u| |
| 26 | + u.username = @test_username |
| 27 | + u.password = @test_password |
| 28 | + u.roles = [ |
| 29 | + Management::Role.new do |r| |
| 30 | + r.name = "data_reader" |
| 31 | + r.bucket = "*" |
| 32 | + end, |
| 33 | + Management::Role.new do |r| |
| 34 | + r.name = "query_select" |
| 35 | + r.bucket = "*" |
| 36 | + end, |
| 37 | + Management::Role.new do |r| |
| 38 | + r.name = "data_writer" |
| 39 | + r.bucket = "*" |
| 40 | + end, |
| 41 | + Management::Role.new do |r| |
| 42 | + r.name = "query_insert" |
| 43 | + r.bucket = "*" |
| 44 | + end, |
| 45 | + Management::Role.new do |r| |
| 46 | + r.name = "query_delete" |
| 47 | + r.bucket = "*" |
| 48 | + end, |
| 49 | + Management::Role.new do |r| |
| 50 | + r.name = "query_manage_index" |
| 51 | + r.bucket = "*" |
| 52 | + end, |
| 53 | + ] |
| 54 | + end |
| 55 | + @cluster.users.upsert_user(@test_user) |
| 56 | + end |
| 57 | + |
| 58 | + def teardown |
| 59 | + connect |
| 60 | + @cluster.users.drop_user(@test_username) |
| 61 | + disconnect |
| 62 | + end |
| 63 | + |
| 64 | + def test_change_password |
| 65 | + skip("#{name}: CAVES does not support change_password") if use_caves? |
| 66 | + |
| 67 | + # Connect to the cluster with the test user |
| 68 | + orig_options = Cluster::ClusterOptions.new |
| 69 | + orig_options.authenticate(@test_username, @test_password) |
| 70 | + @cluster = Cluster.connect(@env.connection_string, orig_options) |
| 71 | + |
| 72 | + # Change the test user's password |
| 73 | + new_password = "a_new_password" |
| 74 | + @cluster.users.change_password(new_password) |
| 75 | + |
| 76 | + # Verify that the connection fails with the old password |
| 77 | + assert_raises(Error::AuthenticationFailure) { Cluster.connect(@env.connection_string, orig_options) } |
| 78 | + |
| 79 | + # Verify that the connection succeeds with the new password |
| 80 | + new_options = Cluster::ClusterOptions.new |
| 81 | + new_options.authenticate(@test_username, new_password) |
| 82 | + @cluster = Cluster.connect(@env.connection_string, new_options) |
| 83 | + |
| 84 | + # Change the password back to the original one |
| 85 | + @cluster.users.change_password(@test_password) |
| 86 | + |
| 87 | + # Verify that the connection fails with the new password |
| 88 | + assert_raises(Error::AuthenticationFailure) { Cluster.connect(@env.connection_string, new_options) } |
| 89 | + end |
| 90 | + end |
| 91 | +end |
0 commit comments