From bdb3172243d983c841cda2b53b49478c0f697203 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Tue, 2 May 2023 16:01:02 +0200
Subject: [PATCH] test(#109): reveal certification renewal issue

---
 pallets/certification/src/tests.rs | 69 ++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)

diff --git a/pallets/certification/src/tests.rs b/pallets/certification/src/tests.rs
index 9b685c6f6..3ad37431d 100644
--- a/pallets/certification/src/tests.rs
+++ b/pallets/certification/src/tests.rs
@@ -175,3 +175,72 @@ fn test_cert_period() {
         ));
     });
 }
+
+// after given validity period, a certification should expire
+#[test]
+fn test_cert_expiration() {
+    new_test_ext(DefaultCertificationConfig {
+        apply_cert_period_at_genesis: false,
+        certs_by_receiver: btreemap![
+            0 => btreemap![
+                1 => Some(5),
+            ],
+        ],
+    })
+    .execute_with(|| {
+        run_to_block(5);
+        // Expiry of cert by issuer 1
+        System::assert_last_event(RuntimeEvent::DefaultCertification(Event::RemovedCert {
+            issuer: 1,
+            issuer_issued_count: 0,
+            receiver: 0,
+            receiver_received_count: 0,
+            expiration: true,
+        }));
+    });
+}
+
+// when renewing a certification, it should not expire now, but later
+#[test]
+fn test_cert_renewal() {
+    new_test_ext(DefaultCertificationConfig {
+        apply_cert_period_at_genesis: false,
+        certs_by_receiver: btreemap![
+            0 => btreemap![
+                1 => Some(5),
+                2 => Some(20),
+            ],
+            1 => btreemap![
+                0 => Some(20),
+                2 => Some(20),
+            ],
+            2 => btreemap![
+                0 => Some(20),
+                1 => Some(20),
+            ],
+        ],
+    })
+    .execute_with(|| {
+        run_to_block(2);
+        // renew certification from bob to alice
+        // this certification should expire 10 blocks later (at block 12)
+        assert_eq!(
+            DefaultCertification::add_cert(RuntimeOrigin::signed(1), 1, 0),
+            Ok(().into())
+        );
+        System::assert_last_event(RuntimeEvent::DefaultCertification(Event::RenewedCert {
+            issuer: 1,
+            receiver: 0,
+        }));
+
+        run_to_block(12);
+        // expiry of previously renewed cert
+        System::assert_last_event(RuntimeEvent::DefaultCertification(Event::RemovedCert {
+            issuer: 1,
+            issuer_issued_count: 1,
+            receiver: 0,
+            receiver_received_count: 1,
+            expiration: true,
+        }));
+    });
+}
-- 
GitLab