Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #67 from neicnordic/test/unit-tests
Browse files Browse the repository at this point in the history
WIP Test/unit tests
  • Loading branch information
teemukataja authored Dec 7, 2021
2 parents 60b4f3f + 397a7c1 commit 2c2107d
Show file tree
Hide file tree
Showing 22 changed files with 2,404 additions and 189 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Tests

on: [push]

jobs:

build:
name: Build
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.16]
steps:

- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Test
run: go test -v -coverprofile=coverage.txt -covermode=atomic ./...

- name: Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.txt
flags: unittests
fail_ci_if_error: false
13 changes: 13 additions & 0 deletions KNOWN_ISSUES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Known Issues and Troubleshooting

## Files metadata endpoint doesn't work with visas
If using GA4GH Visas with `/metadata/datasets/{dataset}/files`, e.g. `/metadata/datasets/https://doi.org/abc/123/files`, a reverse proxy might remove adjacent slashes `//`->`/`.
This has been observed with nginx, with a fix as follows:

[disable slash merging](http://nginx.org/en/docs/http/ngx_http_core_module.html#merge_slashes)
in `server` context
```
server {
merge_slashes off
}
```
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[![CodeQL](https://github.com/neicnordic/sda-download/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/neicnordic/sda-download/actions/workflows/codeql-analysis.yml)
[![Tests](https://github.com/neicnordic/sda-download/actions/workflows/test.yml/badge.svg)](https://github.com/neicnordic/sda-download/actions/workflows/test.yml)
[![Multilinters](https://github.com/neicnordic/sda-download/actions/workflows/report.yml/badge.svg)](https://github.com/neicnordic/sda-download/actions/workflows/report.yml)
[![codecov](https://codecov.io/gh/neicnordic/sda-download/branch/main/graph/badge.svg?token=ZHO4XCDPJO)](https://codecov.io/gh/neicnordic/sda-download)

# SDA Download
`sda-download` is a `go` implementation of the [Data Out API](https://neic-sda.readthedocs.io/en/latest/dataout.html#rest-api-endpoints). The [API Reference](docs/API.md) has example requests and responses.

Expand Down
7 changes: 4 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"time"

"github.com/gorilla/mux"
"github.com/neicnordic/sda-download/api/middleware"
"github.com/neicnordic/sda-download/api/sda"
"github.com/neicnordic/sda-download/internal/config"
Expand All @@ -16,11 +17,11 @@ import (
func Setup() *http.Server {
// Set up routing
log.Info("(2/5) Registering endpoint handlers")
r := http.NewServeMux()
r := mux.NewRouter().SkipClean(true)

r.Handle("/metadata/datasets", middleware.TokenMiddleware(http.HandlerFunc(sda.Datasets)))
r.Handle("/metadata/datasets/", middleware.TokenMiddleware(http.HandlerFunc(sda.Files)))
r.Handle("/files/", middleware.TokenMiddleware(http.HandlerFunc(sda.Download)))
r.Handle("/metadata/datasets/{dataset:[A-Za-z0-9-_.~:/?#@!$&'()*+,;=]+}/files", middleware.TokenMiddleware(http.HandlerFunc(sda.Files)))
r.Handle("/files/{fileid}", middleware.TokenMiddleware(http.HandlerFunc(sda.Download)))

// Configure TLS settings
log.Info("(3/5) Configuring TLS")
Expand Down
27 changes: 27 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package api

import (
"crypto/tls"
"testing"

"github.com/neicnordic/sda-download/internal/config"
)

func TestSetup(t *testing.T) {

// Create web server app
config.Config.App.Host = "localhost"
config.Config.App.Port = 8080
server := Setup()

// Verify that TLS is configured and set for minimum suggested version
if server.TLSConfig.MinVersion < tls.VersionTLS12 {
t.Errorf("server TLS version is too low, expected=%d, got=%d", tls.VersionTLS12, server.TLSConfig.MinVersion)
}

// Verify that server address is correctly read from config
expectedAddress := "localhost:8080"
if server.Addr != expectedAddress {
t.Errorf("server address was not correctly formed, expected=%s, received=%s", expectedAddress, server.Addr)
}
}
9 changes: 2 additions & 7 deletions api/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ func TokenMiddleware(nextHandler http.Handler) http.Handler {
}

// Get permissions
datasets, err = auth.GetPermissions(*visas)
if err != nil {
log.Errorf("failed to parse dataset permission visas, %s", err)
http.Error(w, "visa parsing failed", 500)
return
}
datasets = auth.GetPermissions(*visas)
if len(datasets) == 0 {
log.Debug("token carries no dataset permissions matching the database")
http.Error(w, "no datasets found", 404)
Expand Down Expand Up @@ -94,7 +89,7 @@ func storeDatasets(ctx context.Context, datasets []string) context.Context {
}

// GetDatasets extracts the dataset list from the request context
func GetDatasets(ctx context.Context) []string {
var GetDatasets = func(ctx context.Context) []string {
datasets := ctx.Value("datasets")
if datasets == nil {
log.Debug("request datasets context is empty")
Expand Down
Loading

0 comments on commit 2c2107d

Please sign in to comment.