aboutsummaryrefslogblamecommitdiff
path: root/src/test/java/com/juick/MessageTest.java
blob: 9c079fa37d1b8ae82d5208c020f30e3185a9bfd5 (plain) (tree)
1
2
  
                                 













                                                                           
                  
                              
                               
                                     
                                   
                                      
                                            
                                  
 
                      







                                                    
                                                     

































                                                     
                                                      


















                                                             
                                                                          













                                                                           
                                                                                          













                                                                              
                                                   
                                        
                                                                           

                                   
                                                                           


                                
                                                                           
 
                                                     
 
                                                                           

         
                                                      
                                        
                                                        
 
                                                                                                       
 
                                                             
 
                                                                                                       
 
                                                                     
 
                                                                                                       
     










                                                                                                            





                                                              
                                                                      
     
                                
                                                                                                                                                        
                                                                                                          


                                                                                           


                                                                                                                               
     






                                                                                                                                                                                                                                                                                                               






                                                                                                            









                                                                                                                                                                                                                                                                                                           
                                                                                                                                  

                                                                                                                                               
     




                                                                                                              
 
/*
 * Copyright (C) 2008-2019, Juick
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.juick;

import com.juick.model.Entity;
import com.juick.model.Message;
import com.juick.test.util.MockUtils;
import com.juick.util.MessageUtils;
import com.overzealous.remark.Options;
import com.overzealous.remark.Remark;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

/**
 * Created by aalexeev on 12/7/16.
 */
public class MessageTest {

    @Test
    public void equalsShouldBeReturnCorrectResult() {
        Message message1 = new Message();

        Message message2 = new Message();
        message2.setMid(RandomUtils.nextInt());

        Message message3 = message1;

        assertThat(message1, equalTo(message3));
        assertThat(message3, equalTo(message1));

        assertThat(message1, not(equalTo(message2)));
        assertThat(message2, not(equalTo(message1)));

        int id = RandomUtils.nextInt();
        message1.setMid(id);
        message2.setMid(id);

        id = RandomUtils.nextInt();
        message1.setRid(id);
        message2.setRid(id);

        assertThat(message1, equalTo(message2));
        assertThat(message2, equalTo(message1));

        message1.setMid(RandomUtils.nextInt());
        message1.setRid(RandomUtils.nextInt());

        message2.setMid(RandomUtils.nextInt());
        message2.setRid(RandomUtils.nextInt());

        assertThat(message1, not(equalTo(message2)));
        assertThat(message2, not(equalTo(message1)));
    }

    @Test
    public void compareShouldBeReturnCorrectResult() {
        Message message1 = new Message();

        message1.setMid(RandomUtils.nextInt());
        message1.setRid(RandomUtils.nextInt());

        Message message2 = message1;

        assertThat(message1.compareTo(message2), equalTo(0));
        assertThat(message2.compareTo(message1), equalTo(0));

        Message message3 = new Message();

        message3.setMid(message1.getMid());
        message3.setRid(message1.getRid());

        assertThat(message1.compareTo(message3), equalTo(0));
        assertThat(message3.compareTo(message1), equalTo(0));
    }

    @Test
    public void messageWithGreaterMidShouldBeLessThenMesageWithLessMid() {
        Message message1 = new Message();

        message1.setMid(RandomUtils.nextInt());
        message1.setRid(RandomUtils.nextInt());

        Message message2 = new Message();

        message2.setMid(message1.getMid() + RandomUtils.nextInt(1, 10000));
        message2.setRid(RandomUtils.nextInt());

        assertThat(message1.compareTo(message2), equalTo(1));
        assertThat(message2.compareTo(message1), equalTo(-1));
    }

    @Test
    public void messageWithGreaterRidAndEqualsMidShouldBeGreaterThenMessageWithLessRid() {
        Message message1 = new Message();

        message1.setMid(RandomUtils.nextInt());
        message1.setRid(RandomUtils.nextInt());

        Message message2 = new Message();

        message2.setMid(message1.getMid());
        message2.setRid(message1.getRid() +  + RandomUtils.nextInt(1, 10000));

        assertThat(message1.compareTo(message2), equalTo(-1));
        assertThat(message2.compareTo(message1), equalTo(1));
    }

    @Test
    public void tagsStringShouldBeEmptyIfNoTags() {
        Message message = new Message();

        assertThat(MessageUtils.getTagsString(message), is(emptyString()));

        message.FriendsOnly = true;

        assertThat(MessageUtils.getTagsString(message), is(emptyString()));

        message.Hidden = true;
        message.ReadOnly = true;

        assertThat(MessageUtils.getTagsString(message), is(emptyString()));

        message.setTags(MessageUtils.parseTags(" "));

        assertThat(MessageUtils.getTagsString(message), is(emptyString()));
    }

    @Test
    public void tagsStringShouldHasNoTagDuplicates() {
        Message message = new Message();

        message.setTags(MessageUtils.parseTags("test"));

        assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*test"), equalTo(1));

        message.setTags(MessageUtils.parseTags("test test"));

        assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*test"), equalTo(1));

        message.setTags(MessageUtils.parseTags("test test ab test"));

        assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*test"), equalTo(1));
        assertThat(StringUtils.countMatches(MessageUtils.getTagsString(message), "*ab"), equalTo(1));
    }
    @Test
    public void markdownContentShouldNotHaveUnescapedReplyNumbersBecauseOfTelegram() {
        Message msg = new Message();
        msg.setMid(1);
        msg.setText("See /303 again");
        assertThat(MessageUtils.formatMarkdownText(msg), is("See [/303](https://juick.com/m/1#303) again"));
    }
    @Test
    public void shouldNotThrowIfUrlContainsIllegalCharacters() {
        String msg = "[te](http://juick.com/)[st](http://juick.com/)";
        assertThat(MessageUtils.stripNonSafeUrls(msg), is(msg));
    }
    @Test
    public void mentionsCount() {
        Message msg = new Message();
        msg.setText("@ugnich go home");
        List<String> mentions = MessageUtils.getMentions(msg);
        assertThat(mentions.size(), is(1));
        assertThat(mentions.get(0), is("@ugnich"));
        msg.setText("And dick is @ugnich@jabber.zp.ua");
        assertThat(MessageUtils.getGlobalMentions(msg).size(), is(1));
    }
    @Test
    public void entitiesTest() {
        String msg = "> how ?\nhttp://google.com - there _will_ @ugnich ask questions from #4321, then go to [stackoverflow](http://stackoverflow.com)";
        Message testMessage = MockUtils.mockMessage(514, MockUtils.mockUser(5432, "fmap", "secret"), msg);
        List<Entity> entities = MessageUtils.getEntities(testMessage);
        assertThat(entities.size(), is(6));
        assertThat(entities.stream().filter(e -> e.getType().equals("q")).count(), is(1L));
        assertThat(entities.stream().filter(e -> e.getType().equals("u")).count(), is(1L));
        assertThat(entities.stream().filter(e -> e.getType().equals("a")).count(), is(4L));
        Entity yo = entities.stream().filter(e -> e.getType().equals("q")).findFirst().orElseThrow(IllegalStateException::new);
        assertThat(yo.getText(), is("how ?"));
        assertThat(yo.getStart(), is(0));
        assertThat(yo.getEnd(), is(7));
    }
    @Test
    public void ActivityStreamsHTMLtoMarkdownTest() {
        String text = "<p><span class=\"h-card\"><a href=\"https://juick.com/stanislavv/\" class=\"u-url mention\">@<span>stanislavv</span></a></span> я в <span class=\"h-card\"><a href=\"https://mastodonsocial.ru/@rf\" class=\"u-url mention\">@<span>rf</span></a></span> выкладывал =)</p>";
        Options options = new Options();
        options.inlineLinks = true;
        Remark remark = new Remark(options);
        String parsed = remark.convertFragment(text);
    }
    @Test
    public void messageFormatTest() {
        String msg = "> quote\nmessage";
        assertThat(MessageUtils.formatMessage(msg), is("<q>quote</q>message"));
        String brokenComment = "<!-- read next";
        assertThat(MessageUtils.formatMessage(brokenComment), is("&lt;!-- read next"));
        String url = "[ya](http://ya.ru)";
        assertThat(MessageUtils.formatMessage(url), is("<a href=\"http://ya.ru\" rel=\"nofollow\">ya</a>"));
        String complexMessage = "У футболистов нет мозгов. Что в России, что в Беларуси:\n" +
                "\n" +
                ">Отец футболиста Лухвича, объехавшего пробку по тротуару: «Сына задержали, Infiniti арестовали» https://auto.onliner.by/2019/01/23/probka-9\n" +
                "\n" +
                "Вкратце: малолетний долбоёб ездил по встрече, по тротуарам, парковался где хотел и всё это выкладывал в сеть, мол, хули вы мне сделоете. Сделали. Ибо нехуй.";
        String formattedMessage = "У футболистов нет мозгов. Что в России, что в Беларуси:<br/>\n<br/>\n" +
                "<q>Отец футболиста Лухвича, объехавшего пробку по тротуару: «Сына задержали, Infiniti арестовали» <a href=\"https://auto.onliner.by/2019/01/23/probka-9\" rel=\"nofollow\">auto.onliner.by</a></q>" +
                "<br/>\n" +
                "Вкратце: малолетний долбоёб ездил по встрече, по тротуарам, парковался где хотел и всё это выкладывал в сеть, мол, хули вы мне сделоете. Сделали. Ибо нехуй.";

        assertThat(MessageUtils.formatMessage(complexMessage), is(formattedMessage));
        String multiQuoteWithCarriageReturn = "> quote line 1\r\n> quote line 2\r\nmessage";
        assertThat(MessageUtils.formatMessage(multiQuoteWithCarriageReturn), is("<q>quote line 1<br/>\nquote line 2</q>message"));
        MessageUtils.formatMessage("wget -t 3 -T 10 -w 5 -O - http://www.gismeteo.ru <http://www.gismeteo.ru>/city/daily/4787/ &>/dev/null\n" +
                "\n" +
                "if [ \"$?\" -ne \"0\" ]; then\n");
    }
    @Test
    public void sensitiveTest() {
        Message testMessage = MockUtils.mockMessage(514, MockUtils.mockUser(5432, "rtfmpls", "secret"), "yo");
        testMessage.setTags(MessageUtils.parseTags("NSFW test"));
        assertThat(MessageUtils.isSensitive(testMessage), is(true));
    }
}