go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth_service/impl/servers/imports/server_test.go (about) 1 // Copyright 2023 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package imports 16 17 import ( 18 "bytes" 19 "context" 20 "encoding/json" 21 "io" 22 "net/http" 23 "net/http/httptest" 24 "testing" 25 "time" 26 27 "github.com/julienschmidt/httprouter" 28 29 "go.chromium.org/luci/gae/filter/txndefer" 30 "go.chromium.org/luci/gae/impl/memory" 31 "go.chromium.org/luci/gae/service/datastore" 32 "go.chromium.org/luci/server/auth" 33 "go.chromium.org/luci/server/auth/authtest" 34 "go.chromium.org/luci/server/router" 35 "go.chromium.org/luci/server/tq" 36 37 "go.chromium.org/luci/auth_service/impl/info" 38 "go.chromium.org/luci/auth_service/impl/model" 39 "go.chromium.org/luci/auth_service/testsupport" 40 41 . "github.com/smartystreets/goconvey/convey" 42 . "go.chromium.org/luci/common/testing/assertions" 43 ) 44 45 func TestIngestTarball(t *testing.T) { 46 t.Parallel() 47 48 callEndpoint := func(ctx context.Context, tarballName string, body io.ReadCloser) ([]byte, error) { 49 rw := httptest.NewRecorder() 50 51 rctx := &router.Context{ 52 Writer: rw, 53 Request: (&http.Request{ 54 Method: "PUT", 55 Body: body, 56 }).WithContext(ctx), 57 Params: []httprouter.Param{ 58 { 59 Key: "tarballName", 60 Value: tarballName, 61 }, 62 }, 63 } 64 65 if err := HandleTarballIngestHandler(rctx); err != nil { 66 return nil, err 67 } 68 return rw.Body.Bytes(), nil 69 } 70 71 Convey("Test tarball", t, func() { 72 ctx := auth.WithState(memory.Use(context.Background()), &authtest.FakeState{ 73 Identity: "user:test-user@example.com", 74 }) 75 ctx = info.SetImageVersion(ctx, "test-version") 76 ctx, taskScheduler := tq.TestingContext(txndefer.FilterRDS(ctx), nil) 77 78 // Set up data for test cases. 79 So(datastore.Put(ctx, &model.GroupImporterConfig{ 80 Kind: "GroupImporterConfig", 81 ID: "config", 82 ConfigProto: ` 83 tarball_upload { 84 name: "test_groups.tar.gz" 85 authorized_uploader: "test-user@example.com" 86 systems: "test" 87 } 88 `, 89 ConfigRevision: []byte("some-config-revision"), 90 ModifiedBy: "some-user@example.com", 91 ModifiedTS: time.Date(2021, time.August, 16, 12, 20, 0, 0, time.UTC), 92 }), ShouldBeNil) 93 So(datastore.Put(ctx, &model.AuthDBSnapshotLatest{ 94 Kind: "AuthDBSnapshotLatest", 95 ID: "latest", 96 AuthDBRev: 42, 97 AuthDBSha256: "test-sha", 98 ModifiedTS: time.Date(2021, time.August, 16, 12, 20, 0, 0, time.UTC), 99 }), ShouldBeNil) 100 101 Convey("Empty tarball", func() { 102 _, err := callEndpoint(ctx, "test_groups.tar.gz", io.NopCloser(bytes.NewReader(nil))) 103 So(err, ShouldErrLike, "bad tarball: EOF") 104 }) 105 106 Convey("Happy", func() { 107 tarfile := testsupport.BuildTargz(map[string][]byte{ 108 "at_root": []byte("a\nb"), 109 "test/group-1": []byte("a@example.com\nb@example.com"), 110 "test/group-2": []byte("a@example.com\nb@example.com"), 111 }) 112 113 res, err := callEndpoint(ctx, "test_groups.tar.gz", io.NopCloser(bytes.NewReader(tarfile))) 114 So(err, ShouldBeNil) 115 116 actual := GroupsJSON{} 117 So(json.Unmarshal(res, &actual), ShouldBeNil) 118 119 expected := GroupsJSON{ 120 Groups: []string{ 121 "test/group-1", 122 "test/group-2", 123 }, 124 AuthDBRev: 1, 125 } 126 So(actual, ShouldResemble, expected) 127 So(taskScheduler.Tasks(), ShouldHaveLength, 2) 128 }) 129 }) 130 }