Skip to content

Commit

Permalink
Add tests for Auth API
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyafromrussia authored Oct 30, 2023
2 parents d5e4aa6 + 637e6e2 commit 719158f
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 4 deletions.
6 changes: 4 additions & 2 deletions contract/src/auth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ use crate::{Contract, ContractExt};

#[near_bindgen]
impl AuthApi for Contract {
#[private]
fn add_oracle(&mut self, account_id: AccountId) {
Self::assert_private();

require!(self.oracles.insert(account_id.clone()), "Already exists");
log_str(&format!("Oracle {account_id} was added"));
}

#[private]
fn remove_oracle(&mut self, account_id: AccountId) {
Self::assert_private();

require!(self.oracles.remove(&account_id), "No such oracle");
log_str(&format!("Oracle {account_id} was removed"));
}
Expand Down
3 changes: 2 additions & 1 deletion contract/src/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub(crate) mod api;
pub(crate) mod api;
mod tests;
127 changes: 127 additions & 0 deletions contract/src/auth/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#![cfg(test)]

use model::api::{AuthApi, InitApi};
use near_sdk::{test_utils::VMContextBuilder, testing_env, AccountId};

use crate::Contract;

#[test]
fn add_oracle_by_contract_owner() {
Context::run_test(|mut context, mut contract, accounts| {
context.switch_account(&accounts.owner);
contract.add_oracle(accounts.oracle.clone());

let oracles = contract.get_oracles();
assert_eq!(oracles, vec![accounts.oracle.clone()]);
});
}

#[test]
#[should_panic(expected = "Method is private")]
fn add_oracle_not_by_contract_owner() {
Context::run_test(|mut context, mut contract, accounts| {
context.switch_account(&accounts.alice);
contract.add_oracle(accounts.oracle.clone());
});
}

#[test]
#[should_panic(expected = "Already exists")]
fn add_oracle_twice() {
Context::run_test(|mut context, mut contract, accounts| {
context.switch_account(&accounts.owner);
contract.add_oracle(accounts.oracle.clone());
contract.add_oracle(accounts.oracle.clone());
});
}

#[test]
fn remove_oracle_by_contract_owner() {
Context::run_test(|mut context, mut contract, accounts| {
context.switch_account(&accounts.owner);
contract.add_oracle(accounts.oracle.clone());

let oracles = contract.get_oracles();
assert_eq!(oracles, vec![accounts.oracle.clone()]);

contract.remove_oracle(accounts.oracle.clone());

let oracles = contract.get_oracles();
assert!(oracles.is_empty());
});
}

#[test]
#[should_panic(expected = "Method is private")]
fn remove_oracle_not_by_contract_owner() {
Context::run_test(|mut context, mut contract, accounts| {
contract.oracles.insert(accounts.oracle.clone());

context.switch_account(&accounts.alice);
contract.remove_oracle(accounts.oracle.clone());
});
}

#[test]
#[should_panic(expected = "No such oracle")]
fn remove_not_existing_oracle() {
Context::run_test(|mut context, mut contract, accounts| {
context.switch_account(&accounts.owner);
contract.remove_oracle(accounts.oracle.clone());
});
}

struct Context {
builder: VMContextBuilder,
}

impl Context {
fn run_test<F>(test: F)
where
F: Fn(Context, Contract, &TestAccounts),
{
let accounts = TestAccounts::default();
let token_account = accounts.token.clone();

let mut builder = VMContextBuilder::new();
builder
.current_account_id(accounts.owner.clone())
.signer_account_id(accounts.owner.clone())
.predecessor_account_id(accounts.owner.clone())
.block_timestamp(0);

testing_env!(builder.build());

let contract = Contract::init(token_account);
let context = Context { builder };

test(context, contract, &accounts);
}

pub(crate) fn switch_account(&mut self, account_id: &AccountId) {
self.builder
.predecessor_account_id(account_id.clone())
.signer_account_id(account_id.clone());
testing_env!(self.builder.build());
}
}

struct TestAccounts {
pub alice: AccountId,
pub bob: AccountId,
pub oracle: AccountId,
pub token: AccountId,
pub owner: AccountId,
}

impl Default for TestAccounts {
fn default() -> Self {
Self {
alice: AccountId::new_unchecked("alice".to_string()),
bob: AccountId::new_unchecked("bob".to_string()),
oracle: AccountId::new_unchecked("oracle".to_string()),
token: AccountId::new_unchecked("token".to_string()),
owner: AccountId::new_unchecked("owner".to_string()),
}
}
}
7 changes: 7 additions & 0 deletions contract/src/common/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ impl Contract {
"Unauthorized access"
);
}

pub(crate) fn assert_private() {
require!(
env::current_account_id() == env::predecessor_account_id(),
"Method is private",
);
}
}
3 changes: 2 additions & 1 deletion contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ enum StorageKey {
#[near_bindgen]
impl InitApi for Contract {
#[init]
#[private]
fn init(token_account_id: AccountId) -> Self {
Self::assert_private();

Self {
token_account_id,

Expand Down

0 comments on commit 719158f

Please sign in to comment.