Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ Tests should verify that once the macros provided by this crate are expanded, th
- Compiles and deploys the contract on chain via [NEAR `workspaces`](https://docs.rs/workspaces/0.7.0/workspaces/).
- Sends transactions to the deployed contract to verify plugin functionality.

> **Note**
> Currently some plugins are still tested by unit tests in `near-plugins/src/<plugin_name>.rs`, not by integration tests. Migrating these unit tests to integration tests as described above is WIP.

## Contributors Notes

Traits doesn't contain any implementation, even though some interfaces are self-contained enough to have it.
Expand Down
2 changes: 0 additions & 2 deletions near-plugins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ pub mod events;
pub mod full_access_key_fallback;
pub mod ownable;
pub mod pausable;
#[cfg(not(target_arch = "wasm32"))]
mod test_utils;
pub mod upgradable;

pub use access_control_role::AccessControlRole;
Expand Down
26 changes: 0 additions & 26 deletions near-plugins/src/test_utils.rs

This file was deleted.

226 changes: 2 additions & 224 deletions near-plugins/src/upgradable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
//! [batch transaction]: https://docs.near.org/concepts/basics/transactions/overview
//! [time between scheduling and execution]: https://docs.near.org/sdk/rust/promises/intro
use crate::events::{AsEvent, EventMetadata};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{AccountId, CryptoHash, Promise};
use serde::Serialize;

/// Trait describing the functionality of the _Upgradable_ plugin.
pub trait Upgradable {
Expand Down Expand Up @@ -78,7 +78,7 @@ pub trait Upgradable {
fn up_apply_update_staging_duration(&mut self);
}

#[derive(Serialize)]
#[derive(Deserialize, Serialize)]
pub struct UpgradableDurationStatus {
pub staging_duration: Option<near_sdk::Duration>,
pub staging_timestamp: Option<near_sdk::Timestamp>,
Expand Down Expand Up @@ -125,225 +125,3 @@ impl AsEvent<DeployCode> for DeployCode {
}
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
mod tests {
// TODO: Make simulation test that verifies code is deployed
use crate as near_plugins;
use crate::test_utils::get_context;
use crate::{Ownable, Upgradable};
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::env::sha256;
use near_sdk::{near_bindgen, testing_env, VMContext};
use std::convert::TryInto;

#[near_bindgen]
#[derive(Ownable, Upgradable)]
struct Counter;

#[near_bindgen]
impl Counter {
/// Specify the owner of the contract in the constructor
#[init]
fn new() -> Self {
let mut contract = Self {};
contract.owner_set(Some(near_sdk::env::predecessor_account_id()));
contract
}
}

/// Setup basic account. Owner of the account is `eli.test`
fn setup_basic() -> (Counter, VMContext) {
let ctx = get_context();
testing_env!(ctx.clone());
let mut counter = Counter::new();
counter.owner_set(Some("eli.test".to_string().try_into().unwrap()));
(counter, ctx)
}

#[test]
#[should_panic(expected = r#"Ownable: Method must be called from owner"#)]
fn test_stage_code_not_owner() {
let (mut counter, _) = setup_basic();
counter.up_stage_code(vec![1]);
}

#[test]
fn test_stage_code() {
let (mut counter, mut ctx) = setup_basic();

ctx.predecessor_account_id = "eli.test".to_string().try_into().unwrap();
testing_env!(ctx);

assert_eq!(counter.up_staged_code(), None);
counter.up_stage_code(vec![1]);

assert_eq!(counter.up_staged_code(), Some(vec![1]));

assert_eq!(
counter.up_staged_code_hash(),
Some(sha256(vec![1].as_slice()).try_into().unwrap())
);

counter.up_deploy_code();
}

#[test]
fn test_stage_code_with_delay() {
let (mut counter, mut ctx) = setup_basic();

ctx.predecessor_account_id = "eli.test".to_string().try_into().unwrap();
testing_env!(ctx.clone());

assert_eq!(counter.up_staged_code(), None);

let staging_duration: u64 = std::time::Duration::from_secs(60)
.as_nanos()
.try_into()
.unwrap();
counter.up_init_staging_duration(staging_duration);

let staging_timestamp = ctx.block_timestamp + staging_duration;
counter.up_stage_code(vec![1]);
assert_eq!(
counter.up_get_delay_status().staging_timestamp.unwrap(),
staging_timestamp
);

assert_eq!(counter.up_staged_code(), Some(vec![1]));

ctx.block_timestamp = staging_duration;
testing_env!(ctx);

assert_eq!(
counter.up_staged_code_hash(),
Some(sha256(vec![1].as_slice()).try_into().unwrap())
);

counter.up_deploy_code();
}

#[test]
#[should_panic(expected = "Upgradable: Deploy code too early: staging ends on")]
fn test_panic_stage_code_with_delay() {
let (mut counter, mut ctx) = setup_basic();

ctx.predecessor_account_id = "eli.test".to_string().try_into().unwrap();
testing_env!(ctx.clone());

assert_eq!(counter.up_staged_code(), None);

let staging_duration: u64 = std::time::Duration::from_secs(60)
.as_nanos()
.try_into()
.unwrap();
counter.up_init_staging_duration(staging_duration);

let staging_timestamp = ctx.block_timestamp + staging_duration;
counter.up_stage_code(vec![1]);
assert_eq!(
counter.up_get_delay_status().staging_timestamp.unwrap(),
staging_timestamp
);

assert_eq!(counter.up_staged_code(), Some(vec![1]));

assert_eq!(
counter.up_staged_code_hash(),
Some(sha256(vec![1].as_slice()).try_into().unwrap())
);

ctx.block_timestamp = staging_timestamp - 1;
testing_env!(ctx);

counter.up_deploy_code();
}

#[test]
fn test_update_delay_duration() {
let (mut counter, mut ctx) = setup_basic();

ctx.predecessor_account_id = "eli.test".to_string().try_into().unwrap();
testing_env!(ctx.clone());

assert_eq!(counter.up_staged_code(), None);

let staging_duration: u64 = std::time::Duration::from_secs(60)
.as_nanos()
.try_into()
.unwrap();
let staging_timestamp = ctx.block_timestamp + staging_duration;

counter.up_init_staging_duration(staging_duration);
assert_eq!(
counter.up_get_delay_status().staging_duration.unwrap(),
staging_duration
);

let new_staging_duration = staging_duration + 100;
counter.up_stage_update_staging_duration(new_staging_duration);
assert_eq!(
counter.up_get_delay_status().staging_duration.unwrap(),
staging_duration
);
assert_eq!(
counter
.up_get_delay_status()
.new_staging_duration_timestamp
.unwrap(),
staging_timestamp
);

ctx.block_timestamp = staging_timestamp;
testing_env!(ctx);

counter.up_apply_update_staging_duration();
assert_eq!(
counter.up_get_delay_status().staging_duration.unwrap(),
new_staging_duration
);
}

#[test]
#[should_panic(expected = "Upgradable: Update duration too early: staging ends on ")]
fn test_panic_update_delay_duration() {
let (mut counter, mut ctx) = setup_basic();

ctx.predecessor_account_id = "eli.test".to_string().try_into().unwrap();
testing_env!(ctx.clone());

assert_eq!(counter.up_staged_code(), None);

let staging_duration: u64 = std::time::Duration::from_secs(60)
.as_nanos()
.try_into()
.unwrap();
let staging_timestamp = ctx.block_timestamp + staging_duration;

counter.up_init_staging_duration(staging_duration);
assert_eq!(
counter.up_get_delay_status().staging_duration.unwrap(),
staging_duration
);

let new_staging_duration = staging_duration + 100;
counter.up_stage_update_staging_duration(new_staging_duration);
assert_eq!(
counter.up_get_delay_status().staging_duration.unwrap(),
staging_duration
);
assert_eq!(
counter
.up_get_delay_status()
.new_staging_duration_timestamp
.unwrap(),
staging_timestamp
);

ctx.block_timestamp = staging_timestamp - 1;
testing_env!(ctx);

counter.up_apply_update_staging_duration();
}
}
1 change: 1 addition & 0 deletions near-plugins/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod full_access_key_fallback_contract;
pub mod ownable_contract;
pub mod pausable_contract;
pub mod repo;
pub mod upgradable_contract;
pub mod utils;
Loading