From 83c0095e003a1ad7106b2e3783b0aa63d4b7aef6 Mon Sep 17 00:00:00 2001 From: Influencer Date: Wed, 21 Dec 2016 14:12:05 -0500 Subject: [PATCH] Script to update users password, made test and updated setup.sh (#413) * Added script to update users password, made test and updated setup.sh * Moved update password test to tests.bat * Fixed test for update password --- setup.sh | 5 +++++ target/bin/updatemailuser | 29 +++++++++++++++++++++++++++++ test/tests.bats | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100755 target/bin/updatemailuser diff --git a/setup.sh b/setup.sh index bfdc9da4..2050d755 100755 --- a/setup.sh +++ b/setup.sh @@ -43,6 +43,7 @@ SUBCOMMANDS: email: $0 email add + $0 email update $0 email del $0 email list @@ -115,6 +116,10 @@ case $1 in shift _docker_image addmailuser $@ ;; + update) + shift + _docker_image updatemailuser + ;; del) shift _docker_image delmailuser $@ diff --git a/target/bin/updatemailuser b/target/bin/updatemailuser new file mode 100755 index 00000000..b97ced1f --- /dev/null +++ b/target/bin/updatemailuser @@ -0,0 +1,29 @@ +#!/bin/bash + +DATABASE=/tmp/docker-mailserver/postfix-accounts.cf + +function usage { + echo 'Usage: updatemailuser [password]' + exit 1 +} + +if [ ! -z "$1" ]; then + USER=$1 + if [ -e "$DATABASE" ] && [ -z "$(grep $USER -i $DATABASE)" ]; then + echo "User doesn't exist" + exit 1 + fi + if [ ! -z "$2" ]; then + PASS="$2" + else + read -s -p "Enter Password: " PASS + if [ -z "$PASS" ]; then + echo "Password can't be empty" + exit 1 + fi + fi + ENTRY=$(echo "$USER|$(doveadm pw -s SHA512-CRYPT -u "$USER" -p "$PASS")") + sed -i.bak "s%^$USER.*%$ENTRY%g" $DATABASE +else + usage +fi diff --git a/test/tests.bats b/test/tests.bats index 2fa82550..88790db4 100644 --- a/test/tests.bats +++ b/test/tests.bats @@ -645,6 +645,27 @@ [ -z "$output" ] } +@test "checking user updating password for user in /tmp/docker-mailserver/postfix-accounts.cf" { + docker exec mail /bin/sh -c "addmailuser user3@domain.tld mypassword" + + initialpass=$(run docker exec mail /bin/sh -c "grep user3@domain.tld -i /tmp/docker-mailserver/postfix-accounts.cf") + sleep 2 + docker exec mail /bin/sh -c "updatemailuser user3@domain.tld mynewpassword" + sleep 2 + changepass=$(run docker exec mail /bin/sh -c "grep user3@domain.tld -i /tmp/docker-mailserver/postfix-accounts.cf") + + if [ initialpass != changepass ]; then + status="0" + else + status="1" + fi + + docker exec mail /bin/sh -c "delmailuser user3@domain.tld" + + [ "$status" -eq 0 ] +} + + @test "checking accounts: listmailuser" { run docker exec mail /bin/sh -c "listmailuser | head -n 1" [ "$status" -eq 0 ] @@ -730,6 +751,17 @@ run ./setup.sh -c mail email list [ "$status" -eq 0 ] } +@test "checking setup.sh: setup.sh email update" { + initialpass=$(cat ./config/postfix-accounts.cf | grep lorem@impsum.org | awk -F '|' '{print $2}') + run ./setup.sh -c mail email update lorem@impsum.org consectetur + updatepass=$(cat ./config/postfix-accounts.cf | grep lorem@impsum.org | awk -F '|' '{print $2}') + if [ initialpass != changepass ]; then + status="0" + else + status="1" + fi + [ "$status" -eq 0 ] +} @test "checking setup.sh: setup.sh email del" { run ./setup.sh -c mail email del lorem@impsum.org [ "$status" -eq 0 ]