-
Notifications
You must be signed in to change notification settings - Fork 11
test: add integration tests for Ownable plugin
#59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pub mod access_controllable_contract; | ||
| pub mod ownable_contract; | ||
| pub mod pausable_contract; | ||
| pub mod repo; | ||
| pub mod utils; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| use near_sdk::serde_json::json; | ||
| use workspaces::result::ExecutionFinalResult; | ||
| use workspaces::{Account, AccountId, Contract}; | ||
|
|
||
| /// Wrapper for a contract that is `#[ownable]`. It allows implementing helpers for calling contract | ||
| /// methods. | ||
| pub struct OwnableContract { | ||
| contract: Contract, | ||
| } | ||
|
|
||
| impl OwnableContract { | ||
| pub fn new(contract: Contract) -> Self { | ||
| Self { contract } | ||
| } | ||
|
|
||
| pub fn contract(&self) -> &Contract { | ||
| &self.contract | ||
| } | ||
|
|
||
| pub async fn owner_get(&self, caller: &Account) -> anyhow::Result<Option<AccountId>> { | ||
| let res = caller.call(self.contract.id(), "owner_get").view().await?; | ||
| Ok(res.json::<Option<AccountId>>()?) | ||
| } | ||
|
|
||
| pub async fn owner_set( | ||
| &self, | ||
| caller: &Account, | ||
| owner: Option<AccountId>, | ||
| ) -> workspaces::Result<ExecutionFinalResult> { | ||
| caller | ||
| .call(self.contract.id(), "owner_set") | ||
| .args_json(json!({ "owner": owner })) | ||
| .max_gas() | ||
| .transact() | ||
| .await | ||
| } | ||
|
|
||
| pub async fn owner_is(&self, caller: &Account) -> anyhow::Result<bool> { | ||
| let res = caller | ||
| .call(self.contract.id(), "owner_is") | ||
| .max_gas() | ||
| .transact() | ||
| .await?; | ||
| Ok(res.json::<bool>()?) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| [package] | ||
| name = "ownable" | ||
| version = "0.0.0" | ||
| edition = "2018" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib", "rlib"] | ||
|
|
||
| [dependencies] | ||
| near-plugins = { path = "../../../../near-plugins" } | ||
| near-sdk = "4.1.0" | ||
|
|
||
| [profile.release] | ||
| codegen-units = 1 | ||
| opt-level = "z" | ||
| lto = true | ||
| debug = false | ||
| panic = "abort" | ||
| overflow-checks = true | ||
|
|
||
| [workspace] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| build: | ||
| cargo build --target wasm32-unknown-unknown --release | ||
|
|
||
| # Helpful for debugging. Requires `cargo-expand`. | ||
| expand: | ||
| cargo expand > expanded.rs | ||
|
|
||
| .PHONY: build expand |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [toolchain] | ||
| channel = "1.64.0" | ||
| components = ["clippy", "rustfmt"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| use near_plugins::{only, Ownable}; | ||
| use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; | ||
| use near_sdk::{near_bindgen, AccountId, PanicOnDefault}; | ||
|
|
||
| #[near_bindgen] | ||
| #[derive(Ownable, PanicOnDefault, BorshDeserialize, BorshSerialize)] | ||
| pub struct Counter { | ||
| counter: u64, | ||
| } | ||
|
|
||
| #[near_bindgen] | ||
| impl Counter { | ||
| /// Optionally set the owner in the constructor. | ||
| #[init] | ||
| pub fn new(owner: Option<AccountId>) -> Self { | ||
| let mut contract = Self { counter: 0 }; | ||
| if owner.is_some() { | ||
| contract.owner_set(owner); | ||
| } | ||
| contract | ||
| } | ||
|
|
||
| /// Returns the value of the counter. | ||
| pub fn get_counter(&self) -> u64 { | ||
| self.counter | ||
| } | ||
|
|
||
| /// Anyone may call this method successfully. | ||
| pub fn increase(&mut self) -> u64 { | ||
| self.counter += 1; | ||
| self.counter | ||
| } | ||
|
|
||
| /// _Only_ the owner or the contract itself may call this method successfully. It panics if | ||
| /// anyone else calls it. | ||
| #[only(self, owner)] | ||
| pub fn increase_2(&mut self) -> u64 { | ||
| self.counter += 2; | ||
| self.counter | ||
| } | ||
|
|
||
| /// _Only_ the owner may call this method successfully. It panics if anyone else calls it. | ||
| #[only(owner)] | ||
| pub fn increase_3(&mut self) -> u64 { | ||
| self.counter += 3; | ||
| self.counter | ||
| } | ||
|
|
||
| /// _Only_ the contract itself may call this method successfully. It panics if anyone else calls | ||
| /// it. | ||
| /// | ||
| /// It is possible to use `#[only(self)]` even if the contract does not derive `Ownable`. | ||
| #[only(self)] | ||
| pub fn increase_4(&mut self) -> u64 { | ||
| self.counter += 4; | ||
| self.counter | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
only(self)equivilent to private from near-sdk?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, they are equivalent. I think we should mention that in the docs and recommend using
#[private]rather than#[only(self)], since the latter is provided bynear-sdkdirectly. Would that make sense?I’ve merged this PR since it’s intended to be a port of unit to integration tests. In a follow-up PR I can handle the
selfdocs, if necessary.