Skip to content
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

Empty message with media implementation #113

Merged
merged 9 commits into from
Mar 12, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Version 0.4.0
## Changes
- Improved `alias.go` files (#103)
- Added the support for posting empty-message posts with medias (#110)

# Version 0.3.0
## Changes
Expand Down
115 changes: 114 additions & 1 deletion cli_test/cli_posts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,68 @@ func TestDesmosCLIPostsCreateNoMediasNoPollData(t *testing.T) {
f.Cleanup()
}

func TestDesmosCLIPostsCreateWithMedias(t *testing.T) {
func TestDesmosCLIPostsCreateWithMediasAndEmptyMessage(t *testing.T) {
t.Parallel()
f := InitFixtures(t)

// Start Desmosd server
proc := f.GDStart()
defer proc.Stop(false)

// Save key addresses for later use
fooAddr := f.KeyAddress(keyFoo)

// Later usage variables
subspace := "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e"
message := ""
leobragaz marked this conversation as resolved.
Show resolved Hide resolved
fooAcc := f.QueryAccount(fooAddr)
startTokens := sdk.TokensFromConsensusPower(140)
require.Equal(t, startTokens, fooAcc.GetCoins().AmountOf(denom))

// Create a post
success, _, sterr := f.TxPostsCreate(subspace, message, true, fooAddr, "-y",
"--media https://example.com/media1,text/plain",
"--media https://example.com/media2,application/json")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)

// Make sure the post is saved
storedPosts := f.QueryPosts()
require.NotEmpty(t, storedPosts)
post := storedPosts[0]
require.Equal(t, posts.PostID(1), post.PostID)
require.Nil(t, post.PollData)
require.Len(t, post.Medias, 2)
require.Equal(t, post.Medias, posts.NewPostMedias(
posts.NewPostMedia("https://example.com/media1", "text/plain"),
posts.NewPostMedia("https://example.com/media2", "application/json")))

// Test --dry-run
success, _, _ = f.TxPostsCreate(subspace, message, true, fooAddr, "--dry-run",
"--media https://second.example.com/media1,text/plain",
"--media https://second.example.com/media2,application/json")
require.True(t, success)

// Test --generate-only
success, stdout, stderr := f.TxPostsCreate(subspace, message, true, fooAddr, "--generate-only",
"--media https://third.example.com/media1,text/plain",
"--media https://third.example.com/media2,application/json")
require.Empty(t, stderr)
require.True(t, success)
msg := unmarshalStdTx(f.T, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Len(t, msg.Msgs, 1)
require.Len(t, msg.GetSignatures(), 0)

// Check state didn't change
storedPosts = f.QueryPosts()
require.Len(t, storedPosts, 1)

f.Cleanup()
}

func TestDesmosCLIPostsCreateWithMediasAndNonEmptyMessage(t *testing.T) {
t.Parallel()
f := InitFixtures(t)

Expand Down Expand Up @@ -126,6 +187,58 @@ func TestDesmosCLIPostsCreateWithMedias(t *testing.T) {
f.Cleanup()
}

func TestDesmosCLIPostsCreateWithNoMediasAndNonEmptyMessage(t *testing.T) {
t.Parallel()
f := InitFixtures(t)

// Start Desmosd server
proc := f.GDStart()
defer proc.Stop(false)

// Save key addresses for later use
fooAddr := f.KeyAddress(keyFoo)

// Later usage variables
subspace := "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e"
message := "message"
fooAcc := f.QueryAccount(fooAddr)
startTokens := sdk.TokensFromConsensusPower(140)
require.Equal(t, startTokens, fooAcc.GetCoins().AmountOf(denom))

// Create a post
success, _, sterr := f.TxPostsCreate(subspace, message, true, fooAddr, "-y")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)

// Make sure the post is saved
storedPosts := f.QueryPosts()
require.NotEmpty(t, storedPosts)
post := storedPosts[0]
require.Equal(t, posts.PostID(1), post.PostID)
require.Nil(t, post.PollData)
require.Len(t, post.Medias, 0)

// Test --dry-run
success, _, _ = f.TxPostsCreate(subspace, message, true, fooAddr, "--dry-run")
require.True(t, success)

// Test --generate-only
success, stdout, stderr := f.TxPostsCreate(subspace, message, true, fooAddr, "--generate-only")
require.Empty(t, stderr)
require.True(t, success)
msg := unmarshalStdTx(f.T, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Len(t, msg.Msgs, 1)
require.Len(t, msg.GetSignatures(), 0)

// Check state didn't change
storedPosts = f.QueryPosts()
require.Len(t, storedPosts, 1)

f.Cleanup()
}

func TestDesmosCLIPostsCreateWithPoll(t *testing.T) {
t.Parallel()
f := InitFixtures(t)
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ networks:
ipam:
driver: default
config:
-
subnet: 192.168.10.0/16
- subnet: 192.168.10.0/16
4 changes: 2 additions & 2 deletions x/posts/internal/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (msg MsgCreatePost) ValidateBasic() error {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, fmt.Sprintf("Invalid creator address: %s", msg.Creator))
}

if len(strings.TrimSpace(msg.Message)) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Post message cannot be empty nor blank")
if len(strings.TrimSpace(msg.Message)) == 0 && len(msg.Medias) == 0 {
leobragaz marked this conversation as resolved.
Show resolved Hide resolved
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Post message cannot be empty nor blank when there are no medias")
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
}

if len(msg.Message) > MaxPostMessageLength {
Expand Down
49 changes: 47 additions & 2 deletions x/posts/internal/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestMsgCreatePost_ValidateBasic(t *testing.T) {
error: sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "Invalid creator address: "),
},
{
name: "Empty message returns error",
name: "Empty message returns error if medias and message are empty",
msg: types.NewMsgCreatePost(
"",
types.PostID(0),
Expand All @@ -84,10 +84,55 @@ func TestMsgCreatePost_ValidateBasic(t *testing.T) {
map[string]string{},
creator,
date,
nil,
msgCreatePost.PollData,
),
error: sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Post message cannot be empty nor blank when there are no medias"),
},
{
name: "Non-empty message returns nil if medias are empty",
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
msg: types.NewMsgCreatePost(
"message",
types.PostID(0),
false,
"4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e",
map[string]string{},
creator,
date,
nil,
msgCreatePost.PollData,
),
error: nil,
},
{
name: "Non-empty message returns nil if medias aren't empty",
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
msg: types.NewMsgCreatePost(
"message",
types.PostID(0),
false,
"4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e",
map[string]string{},
creator,
date,
msgCreatePost.Medias,
msgCreatePost.PollData,
),
error: sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Post message cannot be empty nor blank"),
error: nil,
},
{
name: "Empty message returns nil if medias aren't empty",
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
msg: types.NewMsgCreatePost(
"",
types.PostID(0),
false,
"4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e",
map[string]string{},
creator,
date,
msgCreatePost.Medias,
msgCreatePost.PollData,
),
error: nil,
},
{
name: "Very long message returns error",
Expand Down
4 changes: 2 additions & 2 deletions x/posts/internal/types/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ func (p Post) Validate() error {
return fmt.Errorf("invalid post owner: %s", p.Creator)
}

if len(strings.TrimSpace(p.Message)) == 0 {
return fmt.Errorf("post message must be non empty and non blank")
if len(strings.TrimSpace(p.Message)) == 0 && len(p.Medias) == 0 {
return fmt.Errorf("post message cannot be empty nor blank when there are no medias")
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
}

if len(p.Message) > MaxPostMessageLength {
Expand Down
8 changes: 4 additions & 4 deletions x/posts/internal/types/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ func TestPost_Validate(t *testing.T) {
expError: "invalid post owner: ",
},
{
post: types.NewPost(types.PostID(1), types.PostID(0), "", true, "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", map[string]string{}, date, owner).WithMedias(medias).WithPollData(pollData),
expError: "post message must be non empty and non blank",
post: types.NewPost(types.PostID(1), types.PostID(0), "", true, "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", map[string]string{}, date, owner).WithPollData(pollData),
expError: "post message cannot be empty nor blank when there are no medias",
},
{
post: types.NewPost(types.PostID(1), types.PostID(0), " ", true, "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", map[string]string{}, date, owner).WithMedias(medias).WithPollData(pollData),
expError: "post message must be non empty and non blank",
post: types.NewPost(types.PostID(1), types.PostID(0), " ", true, "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", map[string]string{}, date, owner).WithPollData(pollData),
expError: "post message cannot be empty nor blank when there are no medias",
},
{
post: types.NewPost(types.PostID(1), types.PostID(0), "Message", true, "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", map[string]string{}, time.Time{}, owner).WithMedias(medias).WithPollData(pollData),
Expand Down