diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index e2262d48..8fc2c7e0 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -96,6 +96,7 @@ set(TEST_SOURCES
set(TEST_TARGETS
admc_test_object_menu
admc_test_unlock_edit
+ admc_test_upn_edit
)
foreach(target ${TEST_TARGETS})
diff --git a/tests/admc_test_upn_edit.cpp b/tests/admc_test_upn_edit.cpp
new file mode 100644
index 00000000..e082c49d
--- /dev/null
+++ b/tests/admc_test_upn_edit.cpp
@@ -0,0 +1,177 @@
+/*
+ * ADMC - AD Management Center
+ *
+ * Copyright (C) 2020 BaseALT Ltd.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include "admc_test_upn_edit.h"
+
+#include "adldap.h"
+#include "edits/upn_edit.h"
+
+#include
+#include
+#include
+
+#define TEST_SUFFIX "test.com"
+
+void ADMCTestUpnEdit::init() {
+ ADMCTest::init();
+
+ // Embed unlock edit in parent widget
+ QList edits;
+ upn_edit = new UpnEdit(&edits, parent_widget);
+ auto layout = new QFormLayout();
+ parent_widget->setLayout(layout);
+ upn_edit->add_to_layout(layout);
+
+ parent_widget->show();
+ QVERIFY(QTest::qWaitForWindowExposed(parent_widget, 1000));
+
+ prefix_edit = qobject_cast(QApplication::focusWidget());
+ QVERIFY(prefix_edit != nullptr);
+
+ tab();
+ suffix_edit = qobject_cast(QApplication::focusWidget());
+ QVERIFY(suffix_edit != nullptr);
+
+ // Create test user
+ const QString name = TEST_USER;
+ dn = test_object_dn(name, CLASS_USER);
+ const bool create_success = ad.object_add(dn, CLASS_USER);
+ QVERIFY(create_success);
+
+ const QString test_upn = QString("%1@%2").arg(name, TEST_SUFFIX);
+ ad.attribute_replace_string(dn, ATTRIBUTE_USER_PRINCIPAL_NAME, test_upn);
+
+ const AdObject object = ad.search_object(dn);
+ upn_edit->load(ad, object);
+}
+
+// Edit should load prefix and suffix into widgets correctly
+void ADMCTestUpnEdit::test_load() {
+ const QString prefix = prefix_edit->text();
+ QVERIFY(prefix == TEST_USER);
+
+ const QString suffix = suffix_edit->currentText();
+ QVERIFY(suffix == TEST_SUFFIX);
+}
+
+// edited() signal should be emitted when prefix or suffix
+// widgets are edited
+void ADMCTestUpnEdit::test_emit_edited() {
+ bool edited_signal_emitted = false;
+ connect(
+ upn_edit, &AttributeEdit::edited,
+ [&edited_signal_emitted]() {
+ edited_signal_emitted = true;
+ });
+
+ prefix_edit->setText("test");
+ QVERIFY(edited_signal_emitted);
+
+ edited_signal_emitted = false;
+
+ const int suffix_count = suffix_edit->count();
+ QVERIFY((suffix_count > 1));
+ const int changed_index =
+ [this]() {
+ if (suffix_edit->currentIndex() == 0) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }();
+ suffix_edit->setCurrentIndex(changed_index);
+ QVERIFY(edited_signal_emitted);
+}
+
+// Edit should apply changes to suffix
+void ADMCTestUpnEdit::test_apply_suffix() {
+ change_suffix_in_edit();
+
+ const bool apply_success = upn_edit->apply(ad, dn);
+ QVERIFY(apply_success);
+
+ QVERIFY2(edit_state_equals_to_server_state(), "Failed to change upn suffix");
+}
+
+// Edit should apply changes to prefix
+void ADMCTestUpnEdit::test_apply_prefix() {
+ prefix_edit->setText("test-new-prefix");
+
+ const bool apply_success = upn_edit->apply(ad, dn);
+ QVERIFY(apply_success);
+
+ QVERIFY2(edit_state_equals_to_server_state(), "Failed to change upn prefix");
+}
+
+// Edit should apply changes to prefix
+void ADMCTestUpnEdit::test_apply_prefix_and_suffix() {
+ change_suffix_in_edit();
+ prefix_edit->setText("test-new-prefix2");
+
+ const bool apply_success = upn_edit->apply(ad, dn);
+ QVERIFY(apply_success);
+
+ QVERIFY2(edit_state_equals_to_server_state(), "Failed to change upn prefix and suffix");
+}
+
+// Edit should reset to server state after load() call
+void ADMCTestUpnEdit::test_reset() {
+ change_suffix_in_edit();
+ prefix_edit->setText("test-new-prefix3");
+
+ const AdObject object = ad.search_object(dn);
+ upn_edit->load(ad, object);
+
+ QVERIFY2(edit_state_equals_to_server_state(), "Failed to reset");
+}
+
+bool ADMCTestUpnEdit::edit_state_equals_to_server_state() {
+ const AdObject object = ad.search_object(dn);
+ const QString server_upn = object.get_string(ATTRIBUTE_USER_PRINCIPAL_NAME);
+ const QString edit_upn =
+ [this]() {
+ const QString prefix = prefix_edit->text();
+ const QString suffix = suffix_edit->currentText();
+ return QString("%1@%2").arg(prefix, suffix);
+ }();
+
+ return (edit_upn == server_upn);
+}
+
+// Change to next suffix, not equal to current one
+void ADMCTestUpnEdit::change_suffix_in_edit() {
+ const int new_suffix_index =
+ [this]() {
+ const QString current_suffix = suffix_edit->currentText();
+
+ for (int i = 0; i < suffix_edit->count(); i++) {
+ const QString suffix = suffix_edit->itemText(i);
+
+ if (suffix != current_suffix) {
+ return i;
+ }
+ }
+ return -1;
+ }();
+ QVERIFY2((new_suffix_index != -1), "Failed to find different suffix");
+
+ suffix_edit->setCurrentIndex(new_suffix_index);
+}
+
+QTEST_MAIN(ADMCTestUpnEdit)
diff --git a/tests/admc_test_upn_edit.h b/tests/admc_test_upn_edit.h
new file mode 100644
index 00000000..aa9b0fd8
--- /dev/null
+++ b/tests/admc_test_upn_edit.h
@@ -0,0 +1,53 @@
+/*
+ * ADMC - AD Management Center
+ *
+ * Copyright (C) 2020 BaseALT Ltd.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#ifndef ADMC_TEST_UPN_EDIT_H
+#define ADMC_TEST_UPN_EDIT_H
+
+#include "admc_test.h"
+
+class UpnEdit;
+class QLineEdit;
+class QComboBox;
+
+class ADMCTestUpnEdit : public ADMCTest {
+Q_OBJECT
+
+private slots:
+ void init() override;
+
+ void test_load();
+ void test_emit_edited();
+ void test_apply_suffix();
+ void test_apply_prefix();
+ void test_apply_prefix_and_suffix();
+ void test_reset();
+
+private:
+ UpnEdit *upn_edit;
+ QLineEdit *prefix_edit;
+ QComboBox *suffix_edit;
+ QString dn;
+
+ QString get_upn();
+ bool edit_state_equals_to_server_state();
+ void change_suffix_in_edit();
+};
+
+#endif /* ADMC_TEST_UPN_EDIT_H */