Skip to content

Commit

Permalink
switch event to revision published, make mapping public
Browse files Browse the repository at this point in the history
  • Loading branch information
walfly committed Apr 13, 2018
1 parent 6ebdafc commit 9b2b811
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 100 deletions.
22 changes: 3 additions & 19 deletions packages/contracts/contracts/newsroom/Newsroom.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,21 @@ pragma solidity ^0.4.19;
import "./ACL.sol";

contract Newsroom is ACL {
event ContentAdded(address indexed editor, uint indexed id);
event RevisionPublished(address indexed editor, uint indexed id, string uri);
event NameChanged(string newName);

string private constant ROLE_REPORTER = "reporter";
string private constant ROLE_EDITOR = "editor";

uint private latestId;
mapping(uint => Revision) private content;
mapping(uint => Revision) public content;

string public name;

function Newsroom(string newsroomName) ACL() public {
setName(newsroomName);
}

function author(uint contentId) public view returns (address) {
return content[contentId].author;
}

function uri(uint contentId) public view returns (string) {
return content[contentId].uri;
}

function hash(uint contentId) public view returns (bytes32) {
return content[contentId].hash;
}

function timestamp(uint contentId) public view returns (uint) {
return content[contentId].timestamp;
}

function setName(string newName) public onlyOwner() {
require(bytes(newName).length > 0);
name = newName;
Expand Down Expand Up @@ -62,7 +46,7 @@ contract Newsroom is ACL {
0x0
);

ContentAdded(msg.sender, id);
RevisionPublished(msg.sender, id, contentUri);
return id;
}

Expand Down
85 changes: 5 additions & 80 deletions packages/contracts/test/newsroom/newsroom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,84 +21,6 @@ contract("Newsroom", (accounts: string[]) => {
newsroom = await Newsroom.new(FIRST_NEWSROOM_NAME);
});

describe("author", () => {
let id: any;

beforeEach(async () => {
await newsroom.addRole(accounts[1], NEWSROOM_ROLE_EDITOR);
const tx = await newsroom.addContent(SOME_URI, SOME_HASH, { from: accounts[1] });
id = idFromEvent(tx);
});

it("returns 0x0 on non-existent content", async () => {
const is0x0 = is0x0Address(await newsroom.author(9999));
expect(is0x0).to.be.true();
});
// TODO(ritave): add associating author flow
xit("returns proper author", async () => {
await expect(newsroom.author(id, { from: defaultAccount })).to.eventually.be.equal(accounts[1]);
});
});

describe("uri", () => {
let id: any;

beforeEach(async () => {
await newsroom.addRole(defaultAccount, NEWSROOM_ROLE_EDITOR);
const tx = await newsroom.addContent(SOME_URI, SOME_HASH);
id = idFromEvent(tx);
});

it("returns empty string on non-existen content", async () => {
await expect(newsroom.uri(9999)).to.eventually.be.equal("");
});

it("returns proper uri", async () => {
await expect(newsroom.uri(id)).to.eventually.be.equal(SOME_URI);
});
});

describe("timestamp", () => {
let id: any;
let timestamp: any;

beforeEach(async () => {
await newsroom.addRole(defaultAccount, NEWSROOM_ROLE_EDITOR);
const tx = await newsroom.addContent(SOME_URI, SOME_HASH);
id = idFromEvent(tx);
timestamp = await timestampFromTx(web3, tx.receipt);
});

it("returns proper timestamp", async () => {
expect(timestamp).not.to.be.bignumber.equal(0);

await expect(newsroom.timestamp(id)).to.eventually.be.bignumber.equal(timestamp);
});

it("returns zero on not existent content", async () => {
await expect(newsroom.timestamp(9999)).to.eventually.be.bignumber.equal(0);
});
});

describe("hash", () => {
let id: any;

beforeEach(async () => {
await newsroom.addRole(defaultAccount, NEWSROOM_ROLE_EDITOR);
const tx = await newsroom.addContent(SOME_URI, SOME_HASH);
id = idFromEvent(tx);
});

it("returns empty string on non-existen content", async () => {
await expect(newsroom.uri(9999)).to.eventually.be.equal("");
});

it("returns proper hash", async () => {
const hash = await newsroom.hash(id);
await expect(newsroom.hash(id)).to.eventually.be.equal(`${SOME_HASH}`);
});
});

describe("addContent", () => {
it("forbids empty uris", async () => {
await newsroom.addRole(defaultAccount, NEWSROOM_ROLE_EDITOR);
Expand All @@ -112,7 +34,7 @@ contract("Newsroom", (accounts: string[]) => {

it("creates an event", async () => {
const tx = await newsroom.addContent(SOME_URI, SOME_HASH);
const event = findEvent(tx, events.NEWSROOM_ADDED);
const event = findEvent(tx, events.NEWSROOM_PUBLISHED);
expect(event).to.not.be.undefined();
expect(event!.args.editor).to.be.equal(defaultAccount);
});
Expand All @@ -130,7 +52,10 @@ contract("Newsroom", (accounts: string[]) => {
const tx = await newsroom.addContent(SOME_URI, SOME_HASH, { from: accounts[1] });
const id = idFromEvent(tx);

await expect(newsroom.uri(id)).to.eventually.be.equal(SOME_URI);
const [hash, uri, ...rest] = await newsroom.content(id);
console.log({hash, uri, rest});
expect(uri).to.be.equal(SOME_URI);
expect(hash).to.be.equal(`${SOME_HASH}`);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/test/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const events = {
ACL_ROLE_REMOVED: "RoleRemoved",
ACL_SUPERUSER_ADDDED: "SuperuserAdded",
ACL_SUPERUSER_REMOVED: "SuperuserRemoved",
NEWSROOM_ADDED: "ContentAdded",
NEWSROOM_PUBLISHED: "RevisionPublished",
};
export const ACL_TEST_ROLE = "testrole";
export const NEWSROOM_ROLE_REPORTER = "reporter";
Expand Down

0 comments on commit 9b2b811

Please sign in to comment.