| 1 | package daemon |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/FelineStateMachine/tinyrelay/internal/event" |
| 8 | ) |
| 9 | |
| 10 | func TestSocialReactionValueNIP30(t *testing.T) { |
| 11 | url := "https://cdn.example/percent.png" |
| 12 | reaction := event.Event{Content: ":100percent:" , Tags: [][]string{{"emoji" , "100percent" , url}}} |
| 13 | content, image := socialReactionValue(reaction) |
| 14 | if content != ":100percent:" || image != url { |
| 15 | t.Fatalf("custom emoji = %q, %q" , content, image) |
| 16 | } |
| 17 | |
| 18 | for _, invalid := range []event.Event{ |
| 19 | {Content: ":100percent:" , Tags: [][]string{{"emoji" , "100percent" , "javascript:alert(1)" }}}, |
| 20 | {Content: ":100.percent:" , Tags: [][]string{{"emoji" , "100.percent" , url}}}, |
| 21 | {Content: ":unknown:" , Tags: [][]string{{"emoji" , "other" , url}}}, |
| 22 | } { |
| 23 | content, image := socialReactionValue(invalid) |
| 24 | if image != "" || content == "" { |
| 25 | t.Fatalf("invalid custom emoji = %q, %q" , content, image) |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestSocialReactionsGroupCustomEmojiByImage(t *testing.T) { |
| 31 | secret := strings.Repeat("1" , 63) + "2" |
| 32 | other := strings.Repeat("3" , 63) + "4" |
| 33 | third := strings.Repeat("5" , 63) + "6" |
| 34 | actor, err := event.PublicKey(secret) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | post := signedEvent(t, secret, 1, 100, nil, "post" ) |
| 39 | firstURL := "https://cdn.example/one.png" |
| 40 | secondURL := "https://cdn.example/two.png" |
| 41 | extra := []event.Event{ |
| 42 | signedEvent(t, other, 7, 101, [][]string{{"e" , post.ID}, {"emoji" , "party" , firstURL}}, ":party:" ), |
| 43 | signedEvent(t, other, 7, 102, [][]string{{"e" , post.ID}, {"emoji" , "party" , firstURL}}, ":party:" ), |
| 44 | signedEvent(t, third, 7, 102, [][]string{{"e" , post.ID}, {"emoji" , "party" , firstURL}}, ":party:" ), |
| 45 | signedEvent(t, secret, 7, 103, [][]string{{"e" , post.ID}, {"emoji" , "party" , secondURL}}, ":party:" ), |
| 46 | } |
| 47 | items := socialItemsWithRefs([]event.Event{post}, extra, actor, nil) |
| 48 | if len(items) != 1 || len(items[0].Reactions) != 2 { |
| 49 | t.Fatalf("reactions = %#v" , items) |
| 50 | } |
| 51 | if items[0].Reactions[0].Image != firstURL || items[0].Reactions[0].Count != 2 || items[0].Reactions[0].Reacted { |
| 52 | t.Fatalf("first custom reaction = %#v" , items[0].Reactions[0]) |
| 53 | } |
| 54 | if items[0].Reactions[1].Image != secondURL || items[0].Reactions[1].Count != 1 || !items[0].Reactions[1].Reacted { |
| 55 | t.Fatalf("second custom reaction = %#v" , items[0].Reactions[1]) |
| 56 | } |
| 57 | } |