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

Update the post creation CLI command #121

Merged
merged 3 commits into from
Mar 26, 2020
Merged
Changes from 1 commit
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
Next Next commit
edited tx post create CLI cmd and fixed tests
  • Loading branch information
Bragaz authored and Bragaz committed Mar 24, 2020

Verified

This commit was signed with the committer’s verified signature.
wemeetagain Cayman
commit a02e06eb10fb54a39224411a3a5759999d5b854c
36 changes: 18 additions & 18 deletions cli_test/cli_posts_test.go
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ func TestDesmosCLIPostsCreateNoMediasNoPollData(t *testing.T) {
require.Equal(t, startTokens, fooAcc.GetCoins().AmountOf(denom))

// Create a post
success, _, sterr := f.TxPostsCreate(subspace, message, true, fooAddr, "-y")
success, _, sterr := f.TxPostsCreate(subspace, message, fooAddr, "-y")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)
@@ -46,11 +46,11 @@ func TestDesmosCLIPostsCreateNoMediasNoPollData(t *testing.T) {
require.Nil(t, post.Medias)

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

// Test --generate-only
success, stdout, stderr := f.TxPostsCreate(subspace, message, true, fooAddr, "--generate-only=true")
success, stdout, stderr := f.TxPostsCreate(subspace, message, fooAddr, "--generate-only=true")
require.Empty(t, stderr)
require.True(t, success)
msg := unmarshalStdTx(f.T, stdout)
@@ -84,7 +84,7 @@ func TestDesmosCLIPostsCreateWithMediasAndEmptyMessage(t *testing.T) {
require.Equal(t, startTokens, fooAcc.GetCoins().AmountOf(denom))

// Create a post
success, _, sterr := f.TxPostsCreate(subspace, message, true, fooAddr, "-y",
success, _, sterr := f.TxPostsCreate(subspace, message, fooAddr, "-y",
"--media https://example.com/media1,text/plain",
"--media https://example.com/media2,application/json")
require.True(t, success)
@@ -103,13 +103,13 @@ func TestDesmosCLIPostsCreateWithMediasAndEmptyMessage(t *testing.T) {
posts.NewPostMedia("https://example.com/media2", "application/json")))

// Test --dry-run
success, _, _ = f.TxPostsCreate(subspace, message, true, fooAddr, "--dry-run",
success, _, _ = f.TxPostsCreate(subspace, message, 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",
success, stdout, stderr := f.TxPostsCreate(subspace, message, fooAddr, "--generate-only",
"--media https://third.example.com/media1,text/plain",
"--media https://third.example.com/media2,application/json")
require.Empty(t, stderr)
@@ -145,7 +145,7 @@ func TestDesmosCLIPostsCreateWithMediasAndNonEmptyMessage(t *testing.T) {
require.Equal(t, startTokens, fooAcc.GetCoins().AmountOf(denom))

// Create a post
success, _, sterr := f.TxPostsCreate(subspace, message, true, fooAddr, "-y",
success, _, sterr := f.TxPostsCreate(subspace, message, fooAddr, "-y",
"--media https://example.com/media1,text/plain",
"--media https://example.com/media2,application/json")
require.True(t, success)
@@ -164,13 +164,13 @@ func TestDesmosCLIPostsCreateWithMediasAndNonEmptyMessage(t *testing.T) {
posts.NewPostMedia("https://example.com/media2", "application/json")))

// Test --dry-run
success, _, _ = f.TxPostsCreate(subspace, message, true, fooAddr, "--dry-run",
success, _, _ = f.TxPostsCreate(subspace, message, 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",
success, stdout, stderr := f.TxPostsCreate(subspace, message, fooAddr, "--generate-only",
"--media https://third.example.com/media1,text/plain",
"--media https://third.example.com/media2,application/json")
require.Empty(t, stderr)
@@ -206,7 +206,7 @@ func TestDesmosCLIPostsCreateWithNoMediasAndNonEmptyMessage(t *testing.T) {
require.Equal(t, startTokens, fooAcc.GetCoins().AmountOf(denom))

// Create a post
success, _, sterr := f.TxPostsCreate(subspace, message, true, fooAddr, "-y")
success, _, sterr := f.TxPostsCreate(subspace, message, fooAddr, "-y")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)
@@ -220,11 +220,11 @@ func TestDesmosCLIPostsCreateWithNoMediasAndNonEmptyMessage(t *testing.T) {
require.Len(t, post.Medias, 0)

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

// Test --generate-only
success, stdout, stderr := f.TxPostsCreate(subspace, message, true, fooAddr, "--generate-only")
success, stdout, stderr := f.TxPostsCreate(subspace, message, fooAddr, "--generate-only")
require.Empty(t, stderr)
require.True(t, success)
msg := unmarshalStdTx(f.T, stdout)
@@ -258,7 +258,7 @@ func TestDesmosCLIPostsCreateWithPoll(t *testing.T) {
require.Equal(t, startTokens, fooAcc.GetCoins().AmountOf(denom))

// Create a post
success, _, sterr := f.TxPostsCreate(subspace, message, true, fooAddr, "-y",
success, _, sterr := f.TxPostsCreate(subspace, message, fooAddr, "-y",
"--poll-details question=Dog?,multiple-answers=false,allows-answer-edits=true,end-date=2100-01-01T15:00:00.000Z",
"--poll-answer Beagle",
"--poll-answer Pug",
@@ -291,7 +291,7 @@ func TestDesmosCLIPostsCreateWithPoll(t *testing.T) {
require.Equal(t, posts.NewPollAnswer(2, "Shiba"), pollData.ProvidedAnswers[2])

// Test --dry-run
success, _, stderr := f.TxPostsCreate(subspace, message, true, fooAddr, "--dry-run",
success, _, stderr := f.TxPostsCreate(subspace, message, fooAddr, "--dry-run",
"--poll-details question=Dog?,multiple-answers=false,allows-answer-edits=true,end-date=2100-01-01T15:00:00.000Z",
"--poll-answer Beagle",
"--poll-answer Pug",
@@ -300,7 +300,7 @@ func TestDesmosCLIPostsCreateWithPoll(t *testing.T) {
require.True(t, success)

// Test --generate-only
success, stdout, stderr := f.TxPostsCreate(subspace, message, true, fooAddr, "--generate-only",
success, stdout, stderr := f.TxPostsCreate(subspace, message, fooAddr, "--generate-only",
"--poll-details question=Dog?,multiple-answers=false,allows-answer-edits=true,end-date=2100-01-01T15:00:00.000Z",
"--poll-answer Beagle",
"--poll-answer Pug",
@@ -338,7 +338,7 @@ func TestDesmosCLIPostsAnswerPoll(t *testing.T) {
require.Equal(t, startTokens, fooAcc.GetCoins().AmountOf(denom))

// Create a poll with single answer
success, _, sterr := f.TxPostsCreate(subspace, message, true, fooAddr, "-y",
success, _, sterr := f.TxPostsCreate(subspace, message, fooAddr, "-y",
"--poll-details question=Dog?,multiple-answers=false,allows-answer-edits=true,end-date=2100-01-01T15:00:00.000Z",
"--poll-answer Beagle",
"--poll-answer Pug",
@@ -391,7 +391,7 @@ func TestDesmosCLIPostsEdit(t *testing.T) {
fooAddr := f.KeyAddress(keyFoo)

// Create a post
success, _, sterr := f.TxPostsCreate("4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", "message", true, fooAddr, "-y")
success, _, sterr := f.TxPostsCreate("4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e", "message", fooAddr, "-y")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)
@@ -440,7 +440,7 @@ func TestDesmosCLIPostsReactions(t *testing.T) {

// Create a post
success, _, sterr := f.TxPostsCreate("4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e",
"message", true, fooAddr, "-y")
"message", fooAddr, "-y")
require.True(t, success)
require.Empty(t, sterr)
tests.WaitForNextNBlocksTM(1, f.Port)
6 changes: 3 additions & 3 deletions cli_test/test_helpers.go
Original file line number Diff line number Diff line change
@@ -408,9 +408,9 @@ func (f *Fixtures) TxGovVote(proposalID int, option gov.VoteOption, from string,
// desmoscli tx posts

// TxPostsCreate is desmoscli tx posts create
func (f *Fixtures) TxPostsCreate(subspace, message string, allowsComments bool, from sdk.AccAddress, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf(`%s tx posts create %s %s %t --keyring-backend=test --from=%s %v`,
f.DesmosliBinary, subspace, message, allowsComments, from, f.Flags())
func (f *Fixtures) TxPostsCreate(subspace, message string, from sdk.AccAddress, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf(`%s tx posts create %s %s --keyring-backend=test --from=%s %v`,
f.DesmosliBinary, subspace, message, from, f.Flags())
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
}

41 changes: 28 additions & 13 deletions x/posts/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -47,26 +47,38 @@ func GetTxCmd(_ string, cdc *codec.Codec) *cobra.Command {
// GetCmdCreatePost is the CLI command for creating a post
func GetCmdCreatePost(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "create [subspace] [message] [allows-comments]",
Use: "create [subspace] [[message]]",
Short: "Create a new post",
Long: fmt.Sprintf(`
Create a new post specifying the subspace, message and whether or not it will allow for comments.
Create a new post specifying the subspace and the message (optional if any kind of media is provided).
Optional media attachments and polls are also supported. See the below sections to know how to include them.

E.g.
%s tx posts create "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e" "Hello world!" true
%s tx posts create "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e" "Hello world!"

Comments to the post could be locked by including the --allows-comments flag.
By default this field is set to true.

%s tx posts create "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e" "Hello world!" \
--allows-comments false

=== Medias ===
If you want to add one or more media(s) attachment(s), you have to use the --media flag.
You need to firstly specify the media URI and then its mime-type separeted by a comma.

%s tx posts create "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e" "A post with a single media" false \
--media "https://example.com/media1,text/plain"
%s tx posts create "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e" "A post with multiple medias" false \
%s tx posts create "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e" "A post with a single media" \
--media "https://example.com/media1,text/plain" \
--allows-comments false
%s tx posts create "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e" "A post with multiple medias" \
--media "https://example.com/media1,text/plain" \
--media "https://example.com/media2,application/json"

If medias are provided, the post could be created even without any message as following:

%s tx posts create "4e188d9c17150037d5199bbdb91ae1eb2a78a15aca04cb35530cccb81494b36e" \
--media "https://example.com/media1,text/plain" \
--media "https://example.com/media2,application/json" \
--allows-comments false

=== Polls ===
If you want to add a poll to your post you need to specify it through two flags:
@@ -84,17 +96,14 @@ E.g.
--poll-answer "Beagle" \
--poll-answer "Pug" \
--poll-answer "German Sheperd"
`, version.ClientName, version.ClientName, version.ClientName, version.ClientName),
Args: cobra.MinimumNArgs(3),
`, version.ClientName, version.ClientName, version.ClientName, version.ClientName, version.ClientName, version.ClientName),
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
cliCtx := context.NewCLIContextWithInput(inBuf).WithCodec(cdc)

allowsComments, err := strconv.ParseBool(args[2])
if err != nil {
return err
}
allowsComments := viper.GetBool(flagAllowsComments)

parentID, err := types.ParsePostID(viper.GetString(flagParentID))
if err != nil {
@@ -190,8 +199,13 @@ E.g.
}
}

text := ""
if len(args) > 1 {
text = args[1]
}

msg := types.NewMsgCreatePost(
args[1],
text,
parentID,
allowsComments,
args[0],
@@ -206,6 +220,7 @@ E.g.
},
}

cmd.Flags().Bool(flagAllowsComments, true, "Possibility to comment post or not")
cmd.Flags().String(flagParentID, "0", "Id of the post to which this one should be an answer to")
cmd.Flags().StringArray(flagMedia, []string{}, "Current post's media")
cmd.Flags().StringToString(flagPollDetails, map[string]string{}, "Current post's poll details")
2 changes: 1 addition & 1 deletion x/posts/internal/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -247,7 +247,7 @@ func TestMsgCreatePost_ValidateBasic(t *testing.T) {
},
msgCreatePost.PollData,
),
error: sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "uri must be specified and cannot be empty"),
error: sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid uri provided"),
},
{
name: "Invalid URI in message returns error",
18 changes: 3 additions & 15 deletions x/posts/internal/types/post_media_test.go
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ func TestPostMedias_String(t *testing.T) {

actual := postMedias.String()

expected := "Medias - [URI] [Mime-Type]\n[https://uri.com] text/plain \n[https://another.com] application/json"
expected := "URI [Mime-Type]\n[https://uri.com] text/plain \n[https://another.com] application/json"

require.Equal(t, expected, actual)
}
@@ -185,7 +185,7 @@ func TestPostMedias_Validate(t *testing.T) {
MimeType: "text/plain",
},
},
expErr: "uri must be specified and cannot be empty",
expErr: "invalid uri provided",
},

{
@@ -224,18 +224,6 @@ func TestPostMedias_Validate(t *testing.T) {
// --- PostMedia
// -----------

func TestPostMedia_String(t *testing.T) {
pm := types.PostMedia{
URI: "http://example.com",
MimeType: "text/plain",
}

actual := pm.String()
expected := "Media - URI - [http://example.com] ; Mime-Type - [text/plain] \n"

require.Equal(t, expected, actual)
}

func TestPostMedia_Validate(t *testing.T) {
tests := []struct {
postMedia types.PostMedia
@@ -246,7 +234,7 @@ func TestPostMedia_Validate(t *testing.T) {
URI: "",
MimeType: "text/plain",
},
expErr: "uri must be specified and cannot be empty",
expErr: "invalid uri provided",
},
{
postMedia: types.PostMedia{