Skip to content

Commit

Permalink
[api] add endpoint to list active users
Browse files Browse the repository at this point in the history
  • Loading branch information
jbygdell committed Jul 30, 2024
1 parent 268d9c8 commit 2574b55
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sda/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func setup(config *config.Config) *http.Server {
r.POST("/file/accession", isAdmin(), setAccession) // assign accession ID to a file
r.POST("/dataset/create", isAdmin(), createDataset) // maps a set of files to a dataset
r.POST("/dataset/release/*dataset", isAdmin(), releaseDataset) // Releases a dataset to be accessible
r.GET("/users", isAdmin(), listActiveUsers) // Lists all users

cfg := &tls.Config{MinVersion: tls.VersionTLS12}

Expand Down Expand Up @@ -358,3 +359,14 @@ func releaseDataset(c *gin.Context) {

c.Status(http.StatusOK)
}

func listActiveUsers(c *gin.Context) {
users, err := Conf.API.DB.ListActiveUsers()
if err != nil {
log.Debugln("ListActiveUsers failed")
c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error())

return
}
c.JSON(http.StatusOK, users)
}
15 changes: 15 additions & 0 deletions sda/cmd/api/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,18 @@ Admin endpoints are only available to a set of whitelisted users specified in th
```bash
curl -H "Authorization: Bearer $token" -X POST https://HOSTNAME/dataset/release/my-dataset-01
```

- `/users`
- accepts `GET` requests`
- Returns all users with active uploads as a JSON array
Example:
```bash
curl -H "Authorization: Bearer $token" -X GET https://HOSTNAME/users
```

- Error codes
- `200` Query execute ok.
- `401` User is not in the list of admins.
- `500` Internal error due to DB failure.
50 changes: 50 additions & 0 deletions sda/cmd/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,3 +1080,53 @@ func (suite *TestSuite) TestReleaseDataset_NoDataset() {
defer okResponse.Body.Close()
assert.Equal(suite.T(), http.StatusBadRequest, okResponse.StatusCode)
}

func (suite *TestSuite) TestListActiveUsers() {
testUsers := []string{"User-A", "User-B", "User-C"}
for _, user := range testUsers {
for i := 0; i < 3; i++ {
fileID, err := Conf.API.DB.RegisterFile(fmt.Sprintf("/%v/TestGetUserFiles-00%d.c4gh", user, i), user)
if err != nil {
suite.FailNow("failed to register file in database")
}

err = Conf.API.DB.UpdateFileEventLog(fileID, "uploaded", fileID, user, "{}", "{}")
if err != nil {
suite.FailNow("failed to update satus of file in database")
}

stableID := fmt.Sprintf("accession_%s_0%d", user, i)
err = Conf.API.DB.SetAccessionID(stableID, fileID)
if err != nil {
suite.FailNowf("got (%s) when setting stable ID: %s, %s", err.Error(), stableID, fileID)
}
}
}

err = Conf.API.DB.MapFilesToDataset("test-dataset-01", []string{"accession_User-A_00", "accession_User-A_01", "accession_User-A_02"})
if err != nil {
suite.FailNow("failed to map files to dataset")
}

gin.SetMode(gin.ReleaseMode)
assert.NoError(suite.T(), setupJwtAuth())
Conf.API.Admins = []string{"dummy"}

// Mock request and response holders
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/users", http.NoBody)
r.Header.Add("Authorization", "Bearer "+suite.Token)

_, router := gin.CreateTestContext(w)
router.GET("/users", isAdmin(), listActiveUsers)

router.ServeHTTP(w, r)
okResponse := w.Result()
defer okResponse.Body.Close()
assert.Equal(suite.T(), http.StatusOK, okResponse.StatusCode)

var users []string
err = json.NewDecoder(okResponse.Body).Decode(&users)
assert.NoError(suite.T(), err, "failed to list users from DB")
assert.Equal(suite.T(), []string{"User-B", "User-C"}, users)
}

0 comments on commit 2574b55

Please sign in to comment.