2021-06-25 14:32:23 +03:00
/*
* ADMC - AD Management Center
*
* Copyright ( C ) 2020 - 2021 BaseALT Ltd .
* Copyright ( C ) 2020 - 2021 Dmitry Degtyarev
*
* 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 < http : //www.gnu.org/licenses/>.
*/
# include "admc_test_members_tab.h"
# include "select_object_dialog.h"
# include "tabs/membership_tab.h"
# include <QVBoxLayout>
# include <QStandardItemModel>
# include <QPushButton>
# include <QTreeView>
// TODO: test primary functionality
void ADMCTestMembersTab : : init ( ) {
ADMCTest : : init ( ) ;
members_tab = new MembersTab ( ) ;
parent_widget - > show ( ) ;
QVERIFY ( QTest : : qWaitForWindowExposed ( parent_widget , 1000 ) ) ;
auto layout = new QVBoxLayout ( ) ;
parent_widget - > setLayout ( layout ) ;
layout - > addWidget ( members_tab ) ;
view = members_tab - > findChild < QTreeView * > ( ) ;
QVERIFY ( view ! = nullptr ) ;
model = members_tab - > findChild < QStandardItemModel * > ( ) ;
QVERIFY ( model ! = nullptr ) ;
add_button = members_tab - > findChild < QPushButton * > ( " add_button " ) ;
QVERIFY ( add_button ! = nullptr ) ;
remove_button = members_tab - > findChild < QPushButton * > ( " remove_button " ) ;
QVERIFY ( remove_button ! = nullptr ) ;
// Create test user
const QString user_name = TEST_USER ;
user_dn = test_object_dn ( user_name , CLASS_USER ) ;
const bool create_user_success = ad . object_add ( user_dn , CLASS_USER ) ;
QVERIFY ( create_user_success ) ;
// Create test group
const QString group_name = TEST_GROUP ;
group_dn = test_object_dn ( group_name , CLASS_GROUP ) ;
const bool create_group_success = ad . object_add ( group_dn , CLASS_GROUP ) ;
QVERIFY ( create_group_success ) ;
// Load it into the tab
const AdObject object = ad . search_object ( group_dn ) ;
members_tab - > load ( ad , object ) ;
}
// Loading a group without members should result in empty
// model
void ADMCTestMembersTab : : load_empty ( ) {
QVERIFY ( model - > rowCount ( ) = = 0 ) ;
}
// Loading a group with members should put members in the
// model
void ADMCTestMembersTab : : load ( ) {
const bool add_success = ad . group_add_member ( group_dn , user_dn ) ;
QVERIFY ( add_success ) ;
const AdObject object = ad . search_object ( group_dn ) ;
members_tab - > load ( ad , object ) ;
QVERIFY ( model - > rowCount ( ) = = 1 ) ;
auto item = model - > item ( 0 , 0 ) ;
QVERIFY ( item - > text ( ) = = dn_get_name ( user_dn ) ) ;
}
// Removing members should remove members from model and group
void ADMCTestMembersTab : : remove ( ) {
load ( ) ;
const QModelIndex index = model - > index ( 0 , 0 ) ;
view - > setCurrentIndex ( index ) ;
remove_button - > click ( ) ;
members_tab - > apply ( ad , group_dn ) ;
const AdObject updated_object = ad . search_object ( group_dn ) ;
const QList < QString > member_list = updated_object . get_strings ( ATTRIBUTE_MEMBER ) ;
QVERIFY ( model - > rowCount ( ) = = 0 ) ;
QVERIFY ( member_list . isEmpty ( ) ) ;
}
void ADMCTestMembersTab : : add ( ) {
add_button - > click ( ) ;
auto select_dialog = members_tab - > findChild < SelectObjectDialog * > ( ) ;
select_in_select_dialog ( select_dialog , user_dn ) ;
2021-06-28 12:13:51 +03:00
select_dialog - > accept ( ) ;
2021-06-25 14:32:23 +03:00
// Check ui state before applying
QVERIFY ( model - > rowCount ( ) = = 1 ) ;
QVERIFY ( model - > item ( 0 , 0 ) - > text ( ) = = dn_get_name ( user_dn ) ) ;
// Apply and check object state
members_tab - > apply ( ad , group_dn ) ;
const AdObject object = ad . search_object ( group_dn ) ;
const QList < QString > member_list = object . get_strings ( ATTRIBUTE_MEMBER ) ;
QVERIFY ( member_list = = QList < QString > ( { user_dn } ) ) ;
// Check ui state after applying (just in case)
QVERIFY ( model - > rowCount ( ) = = 1 ) ;
QVERIFY ( model - > item ( 0 , 0 ) - > text ( ) = = dn_get_name ( user_dn ) ) ;
}
QTEST_MAIN ( ADMCTestMembersTab )