github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/utils/utils_test.go (about) 1 package utils_test 2 3 import ( 4 "errors" 5 "os" 6 "strconv" 7 "strings" 8 "testing" 9 10 "github.com/ngocphuongnb/tetua/app/test" 11 "github.com/ngocphuongnb/tetua/app/utils" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestSanitizePlainText(t *testing.T) { 16 assert.Equal(t, "test", utils.SanitizePlainText("<script>alert('test')</script><p>test</p>")) 17 } 18 19 func TestSanitizeMarkdown(t *testing.T) { 20 assert.Equal(t, 21 "# Title\n**Code**\n\n```<?php echo 1; ?>```\n<iframe src=\"https://www.youtube.com/embed/dQw4w9WgXcQ\" frameborder=\"0\" allowfullscreen=\"\" sandbox=\"\"></iframe>\n<iframe frameborder=\"0\" allowfullscreen=\"\" sandbox=\"\"></iframe>", 22 utils.SanitizeMarkdown("# Title\n**Code**\n\n```<?php echo 1; ?>```\n<iframe src=\"https://www.youtube.com/embed/dQw4w9WgXcQ\" frameborder=\"0\" allowfullscreen=\"\" sandbox=\"\"></iframe>\n<iframe src=\"https://danger.local\" frameborder=\"0\" allowfullscreen=\"\" sandbox=\"\"></iframe>"), 23 ) 24 } 25 26 func TestExtractContent(t *testing.T) { 27 title, content := utils.ExtractContent("# Title\n## Content") 28 assert.Equal(t, "Title", title) 29 assert.Equal(t, "## Content", content) 30 } 31 32 func TestMarkdownToHtml(t *testing.T) { 33 html, err := utils.MarkdownToHtml("# Title\n## Content") 34 assert.NoError(t, err) 35 assert.Equal(t, "<h1 id=\"title\">Title</h1>\n<h2 id=\"content\">Content</h2>\n", html) 36 } 37 38 func TestGenerateHash(t *testing.T) { 39 hash, err := utils.GenerateHash("") 40 assert.Equal(t, "hash: input cannot be empty", err.Error()) 41 assert.Equal(t, "", hash) 42 43 hash, err = utils.GenerateHash("input") 44 assert.NoError(t, err) 45 assert.Equal(t, true, hash != "") 46 47 err = utils.CheckHash("input", "") 48 assert.Equal(t, "invalid hash", err.Error()) 49 50 err = utils.CheckHash("input", "$argon2id$v=a$m=a,t=a,p=a$OamJg0HQDzROGA8uxX6QtA$B3Ei9eglsBPiyCrxLGdqV2KeYIRTpfsvBLH4GSOEw8M") 51 assert.Equal(t, "expected integer", err.Error()) 52 53 err = utils.CheckHash("input", "$argon2id$v=19$m=65536,t=3,p=4$invalid_salt$B3Ei9eglsBPiyCrxLGdqV2KeYIRTpfsvBLH4GSOEw8M") 54 assert.Equal(t, "illegal base64 data at input byte 7", err.Error()) 55 56 err = utils.CheckHash("input", "$argon2id$v=19$m=65536,t=3,p=4$OamJg0HQDzROGA8uxX6QtA$invalid_hash") 57 assert.Equal(t, "illegal base64 data at input byte 7", err.Error()) 58 59 err = utils.CheckHash("input2", "$argon2id$v=19$m=65536,t=3,p=4$OamJg0HQDzROGA8uxX6QtA$B3Ei9eglsBPiyCrxLGdqV2KeYIRTpfsvBLH4GSOEw8M") 60 assert.Equal(t, "invalid hash", err.Error()) 61 62 err = utils.CheckHash("input", hash) 63 assert.NoError(t, err) 64 } 65 66 func TestGetFunctionName(t *testing.T) { 67 assert.Equal(t, true, strings.HasSuffix(utils.GetFunctionName(TestGenerateHash), "utils_test.TestGenerateHash")) 68 defer test.RecoverPanic(t, "reflect: call of reflect.Value.Pointer on zero Value", "nil input") 69 assert.Equal(t, "", utils.GetFunctionName(nil)) 70 } 71 72 func TestSliceOverlap(t *testing.T) { 73 assert.Equal(t, []int{2, 3}, utils.SliceOverlap([]int{1, 2, 3}, []int{2, 3, 4})) 74 assert.Equal(t, []int{}, utils.SliceOverlap([]int{1, 2, 3}, []int{4, 5, 6})) 75 } 76 77 func TestSliceFilter(t *testing.T) { 78 assert.Equal(t, []int{2, 3}, utils.SliceFilter([]int{1, 2, 3}, func(i int) bool { return i > 1 })) 79 } 80 81 func TestSliceMap(t *testing.T) { 82 assert.Equal(t, []string{"1", "2", "3"}, utils.SliceMap([]int{1, 2, 3}, func(i int) string { return strconv.Itoa(i) })) 83 } 84 85 func TestRepeat(t *testing.T) { 86 assert.Equal(t, []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, utils.Repeat(1, 10)) 87 } 88 89 func TestSliceAppendIfNotExists(t *testing.T) { 90 assert.Equal(t, []int{1, 2, 3, 4}, utils.SliceAppendIfNotExists([]int{1, 2, 3}, 4, func(i int) bool { 91 return i == 4 92 })) 93 assert.Equal(t, []int{1, 2, 3}, utils.SliceAppendIfNotExists([]int{1, 2, 3}, 2, func(i int) bool { 94 return i == 2 95 })) 96 } 97 98 func TestRecoverPanic(t *testing.T) { 99 defer test.RecoverPanic(t, "panic", "panic") 100 panic("panic") 101 } 102 103 func TestGetRootDir(t *testing.T) { 104 assert.Equal(t, true, strings.HasSuffix(test.GetRootDir(), "go/src/testing")) 105 } 106 107 func TestCreateTestDir(t *testing.T) { 108 dir := test.CreateDir("testing-dir-") 109 defer os.RemoveAll(dir) 110 assert.Equal(t, true, strings.Contains(dir, "testing-dir-")) 111 } 112 113 func TestGetStructField(t *testing.T) { 114 type testStruct struct { 115 Field1 string 116 Field2 int 117 } 118 var s testStruct = testStruct{ 119 Field1: "field1 value", 120 Field2: 2, 121 } 122 assert.Equal(t, "field1 value", utils.GetStructField(s, "Field1").String()) 123 assert.Equal(t, 2, int(utils.GetStructField(s, "Field2").Int())) 124 } 125 126 func TestFirstError(t *testing.T) { 127 err1 := errors.New("error1") 128 err2 := errors.New("error2") 129 assert.Equal(t, nil, utils.FirstError(nil)) 130 assert.Equal(t, err1, utils.FirstError(nil, err1)) 131 assert.Equal(t, err2, utils.FirstError(nil, err2, err1)) 132 }