code.gitea.io/gitea@v1.22.3/modules/setting/attachment_test.go (about) 1 // Copyright 2023 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package setting 5 6 import ( 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func Test_getStorageCustomType(t *testing.T) { 13 iniStr := ` 14 [attachment] 15 STORAGE_TYPE = my_minio 16 MINIO_BUCKET = gitea-attachment 17 18 [storage.my_minio] 19 STORAGE_TYPE = minio 20 MINIO_ENDPOINT = my_minio:9000 21 ` 22 cfg, err := NewConfigProviderFromData(iniStr) 23 assert.NoError(t, err) 24 25 assert.NoError(t, loadAttachmentFrom(cfg)) 26 27 assert.EqualValues(t, "minio", Attachment.Storage.Type) 28 assert.EqualValues(t, "my_minio:9000", Attachment.Storage.MinioConfig.Endpoint) 29 assert.EqualValues(t, "gitea-attachment", Attachment.Storage.MinioConfig.Bucket) 30 assert.EqualValues(t, "attachments/", Attachment.Storage.MinioConfig.BasePath) 31 } 32 33 func Test_getStorageTypeSectionOverridesStorageSection(t *testing.T) { 34 iniStr := ` 35 [attachment] 36 STORAGE_TYPE = minio 37 38 [storage.minio] 39 MINIO_BUCKET = gitea-minio 40 41 [storage] 42 MINIO_BUCKET = gitea 43 ` 44 cfg, err := NewConfigProviderFromData(iniStr) 45 assert.NoError(t, err) 46 47 assert.NoError(t, loadAttachmentFrom(cfg)) 48 49 assert.EqualValues(t, "minio", Attachment.Storage.Type) 50 assert.EqualValues(t, "gitea-minio", Attachment.Storage.MinioConfig.Bucket) 51 assert.EqualValues(t, "attachments/", Attachment.Storage.MinioConfig.BasePath) 52 } 53 54 func Test_getStorageSpecificOverridesStorage(t *testing.T) { 55 iniStr := ` 56 [attachment] 57 STORAGE_TYPE = minio 58 MINIO_BUCKET = gitea-attachment 59 60 [storage.attachments] 61 MINIO_BUCKET = gitea 62 63 [storage] 64 STORAGE_TYPE = local 65 ` 66 cfg, err := NewConfigProviderFromData(iniStr) 67 assert.NoError(t, err) 68 69 assert.NoError(t, loadAttachmentFrom(cfg)) 70 71 assert.EqualValues(t, "minio", Attachment.Storage.Type) 72 assert.EqualValues(t, "gitea-attachment", Attachment.Storage.MinioConfig.Bucket) 73 assert.EqualValues(t, "attachments/", Attachment.Storage.MinioConfig.BasePath) 74 } 75 76 func Test_getStorageGetDefaults(t *testing.T) { 77 cfg, err := NewConfigProviderFromData("") 78 assert.NoError(t, err) 79 80 assert.NoError(t, loadAttachmentFrom(cfg)) 81 82 // default storage is local, so bucket is empty 83 assert.EqualValues(t, "", Attachment.Storage.MinioConfig.Bucket) 84 } 85 86 func Test_getStorageInheritNameSectionType(t *testing.T) { 87 iniStr := ` 88 [storage.attachments] 89 STORAGE_TYPE = minio 90 ` 91 cfg, err := NewConfigProviderFromData(iniStr) 92 assert.NoError(t, err) 93 94 assert.NoError(t, loadAttachmentFrom(cfg)) 95 96 assert.EqualValues(t, "minio", Attachment.Storage.Type) 97 } 98 99 func Test_AttachmentStorage(t *testing.T) { 100 iniStr := ` 101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 102 [storage] 103 STORAGE_TYPE = minio 104 MINIO_ENDPOINT = s3.my-domain.net 105 MINIO_BUCKET = gitea 106 MINIO_LOCATION = homenet 107 MINIO_USE_SSL = true 108 MINIO_ACCESS_KEY_ID = correct_key 109 MINIO_SECRET_ACCESS_KEY = correct_key 110 ` 111 cfg, err := NewConfigProviderFromData(iniStr) 112 assert.NoError(t, err) 113 114 assert.NoError(t, loadAttachmentFrom(cfg)) 115 storage := Attachment.Storage 116 117 assert.EqualValues(t, "minio", storage.Type) 118 assert.EqualValues(t, "gitea", storage.MinioConfig.Bucket) 119 } 120 121 func Test_AttachmentStorage1(t *testing.T) { 122 iniStr := ` 123 [storage] 124 STORAGE_TYPE = minio 125 ` 126 cfg, err := NewConfigProviderFromData(iniStr) 127 assert.NoError(t, err) 128 129 assert.NoError(t, loadAttachmentFrom(cfg)) 130 assert.EqualValues(t, "minio", Attachment.Storage.Type) 131 assert.EqualValues(t, "gitea", Attachment.Storage.MinioConfig.Bucket) 132 assert.EqualValues(t, "attachments/", Attachment.Storage.MinioConfig.BasePath) 133 }