Skip to content
Snippets Groups Projects
tests.rs 3.52 KiB
Newer Older
Éloïs's avatar
Éloïs committed
// Copyright 2021 Axiom-Team
//
// This file is part of Substrate-Libre-Currency.
//
// Substrate-Libre-Currency is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License.
//
// Substrate-Libre-Currency 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.

use crate::mock::IdtyDid as Did;
use crate::mock::IdtyRight as Right;
use crate::mock::*;
use crate::Error;
use frame_support::assert_err;
use frame_support::assert_ok;
use frame_system::{EventRecord, Phase};

#[test]
fn test_no_identity() {
    let identities = Vec::with_capacity(0);
Éloïs's avatar
Éloïs committed
    new_test_ext(IdentityConfig { identities }).execute_with(|| {
        assert_eq!(Identity::identities_count(), 0);
    });
}

#[test]
fn test_two_identities() {
Éloïs's avatar
Éloïs committed
        crate::IdtyValue {
            expire_on: 5,
Éloïs's avatar
Éloïs committed
            owner_key: 1,
            removable_on: 0,
            renewable_on: 3,
Éloïs's avatar
Éloïs committed
            rights: vec![(Right::Right2, Some(10))],
            status: crate::IdtyStatus::Validated,
Éloïs's avatar
Éloïs committed
            data: (),
Éloïs's avatar
Éloïs committed
        },
        crate::IdtyValue {
            expire_on: 5,
Éloïs's avatar
Éloïs committed
            owner_key: 2,
            removable_on: 0,
            renewable_on: 3,
Éloïs's avatar
Éloïs committed
            rights: vec![(Right::Right1, Some(20))],
            status: crate::IdtyStatus::Validated,
Éloïs's avatar
Éloïs committed
            data: (),
Éloïs's avatar
Éloïs committed
        },
Éloïs's avatar
Éloïs committed
    new_test_ext(IdentityConfig { identities }).execute_with(|| {
        // Should have two identities
        assert_eq!(Identity::identities_count(), 2);

        // We need to initialize at least one block before any call
        run_to_block(1);

        // Add right Right1 for Did(0)
Éloïs's avatar
Éloïs committed
        // Should succes and trigger the correct event
Éloïs's avatar
Éloïs committed
        assert_ok!(Identity::add_right(Origin::root(), 1, Right::Right1));
Éloïs's avatar
Éloïs committed
        let events = System::events();
        assert_eq!(events.len(), 1);
        assert_eq!(
            events[0],
            EventRecord {
                phase: Phase::Initialization,
                event: Event::Identity(crate::Event::IdtyAcquireRight(Did(0), Right::Right1)),
Éloïs's avatar
Éloïs committed
                topics: vec![],
            }
        );
        // Add right Right2 for Did(0)
        // Should fail because Did(0) already have this right
Éloïs's avatar
Éloïs committed
        assert_err!(
Éloïs's avatar
Éloïs committed
            Identity::add_right(Origin::root(), 1, Right::Right2),
Éloïs's avatar
Éloïs committed
            Error::<Test>::RightAlreadyAdded
        );

        run_to_block(3);

        // Delete right Right1 for Did(1)
Éloïs's avatar
Éloïs committed
        // Should succes and trigger the correct event
Éloïs's avatar
Éloïs committed
        assert_ok!(Identity::del_right(Origin::root(), 2, Right::Right1));
Éloïs's avatar
Éloïs committed
        let events = System::events();
        assert_eq!(events.len(), 2);
        assert_eq!(
            events[1],
            EventRecord {
                phase: Phase::Initialization,
                event: Event::Identity(crate::Event::IdtyLostRight(Did(1), Right::Right1)),
Éloïs's avatar
Éloïs committed
                topics: vec![],
            }
        );

        // The Did(1) identity has no more rights, the inactivity period must start to run
Éloïs's avatar
Éloïs committed
        let idty2 = Identity::identity(2);
Éloïs's avatar
Éloïs committed
        assert!(idty2.rights.is_empty());
        assert_eq!(idty2.removable_on, 7);
Éloïs's avatar
Éloïs committed
    });
}