1414import com .slack .api .methods .response .chat .ChatPostMessageResponse ;
1515import com .slack .api .methods .response .chat .ChatUpdateResponse ;
1616import com .slack .api .methods .response .files .*;
17+ import com .slack .api .methods .response .users .UsersConversationsResponse ;
1718import com .slack .api .model .Conversation ;
19+ import com .slack .api .model .ConversationType ;
1820import config .Constants ;
1921import config .SlackTestConfig ;
2022import lombok .extern .slf4j .Slf4j ;
3133
3234import static org .hamcrest .CoreMatchers .*;
3335import static org .hamcrest .MatcherAssert .assertThat ;
36+ import static org .hamcrest .Matchers .greaterThan ;
3437
3538@ Slf4j
3639public class files_Test {
@@ -434,7 +437,7 @@ public void issue523_image_no_filename() throws IOException, SlackApiException {
434437 }
435438
436439 @ Test
437- public void uploadAndPostMessage () throws IOException , SlackApiException {
440+ public void uploadAndPostMessage () throws IOException , SlackApiException , InterruptedException {
438441 MethodsClient slackMethods = slack .methods (userToken );
439442
440443 ChatPostMessageResponse message = slackMethods .chatPostMessage (r -> r
@@ -445,6 +448,14 @@ public void uploadAndPostMessage() throws IOException, SlackApiException {
445448
446449 String channelId = message .getChannel ();
447450
451+ // Share the file in a private channel
452+ UsersConversationsResponse userPrivateChannels = slackMethods .usersConversations (r -> r
453+ .types (Arrays .asList (ConversationType .PRIVATE_CHANNEL )));
454+ assertThat (userPrivateChannels .getError (), is (nullValue ()));
455+ assertThat (userPrivateChannels .getChannels ().size (), is (greaterThan (0 )));
456+
457+ String privateChannelId = userPrivateChannels .getChannels ().get (0 ).getId ();
458+
448459 File file = new File ("src/test/resources/sample.txt" );
449460 com .slack .api .model .File fileObj ;
450461 {
@@ -477,6 +488,36 @@ public void uploadAndPostMessage() throws IOException, SlackApiException {
477488 .build ());
478489 assertThat (newMessage .getError (), is (nullValue ()));
479490 }
491+
492+ {
493+ ChatPostMessageResponse newMessage = slackMethods .chatPostMessage (ChatPostMessageRequest .builder ()
494+ .channel (privateChannelId )
495+ .text (fileObj .getPermalink ())
496+ .unfurlLinks (true )
497+ .unfurlMedia (true )
498+ .build ());
499+ assertThat (newMessage .getError (), is (nullValue ()));
500+ }
501+
502+ Thread .sleep (2_000L );
503+
504+ // verify if the shares is expected
505+ FilesInfoResponse fileInfo = slackMethods .filesInfo (r -> r .file (fileObj .getId ()));
506+ assertThat (fileInfo .getError (), is (nullValue ()));
507+ assertThat (fileInfo .getFile ().getShares ().getPublicChannels (), is (notNullValue ()));
508+ assertThat (fileInfo .getFile ().getShares ().getPrivateChannels (), is (notNullValue ()));
509+
510+ FilesUploadResponse uploadAndSharedInTwoTypesOfChannels = slackMethods .filesUpload (r -> r
511+ .file (file )
512+ .initialComment ("test" )
513+ .filetype ("text" )
514+ .filename ("sample.txt" )
515+ .title ("file title" )
516+ .channels (Arrays .asList (channelId , privateChannelId ))
517+ );
518+ assertThat (uploadAndSharedInTwoTypesOfChannels .getError (), is (nullValue ()));
519+ assertThat (uploadAndSharedInTwoTypesOfChannels .getFile ().getShares ().getPublicChannels (), is (notNullValue ()));
520+ assertThat (uploadAndSharedInTwoTypesOfChannels .getFile ().getShares ().getPrivateChannels (), is (notNullValue ()));
480521 }
481522
482523 @ Test
0 commit comments