Create environmental var package

This commit is contained in:
Daniel Krol 2022-07-23 18:13:56 -04:00
parent 36659ef720
commit ade526f4f9
10 changed files with 45 additions and 21 deletions

16
env/env.go vendored Normal file
View file

@ -0,0 +1,16 @@
package env
import (
"os"
)
// For test stubs
type EnvInterface interface {
Getenv(key string) string
}
type Env struct{}
func (e *Env) Getenv(key string) string {
return os.Getenv(key)
}

View file

@ -4,6 +4,7 @@ import (
"log" "log"
"lbryio/lbry-id/auth" "lbryio/lbry-id/auth"
"lbryio/lbry-id/env"
"lbryio/lbry-id/server" "lbryio/lbry-id/server"
"lbryio/lbry-id/store" "lbryio/lbry-id/store"
) )
@ -23,6 +24,6 @@ func storeInit() (s store.Store) {
func main() { func main() {
store := storeInit() store := storeInit()
srv := server.Init(&auth.Auth{}, &store) srv := server.Init(&auth.Auth{}, &store, &env.Env{})
srv.Serve() srv.Serve()
} }

View file

@ -13,9 +13,8 @@ import (
) )
func TestServerRegisterSuccess(t *testing.T) { func TestServerRegisterSuccess(t *testing.T) {
testAuth := TestAuth{} testStore := &TestStore{}
testStore := TestStore{} s := Server{&TestAuth{}, testStore, &TestEnv{}}
s := Server{&testAuth, &testStore}
requestBody := []byte(`{"email": "abc@example.com", "password": "123", "clientSaltSeed": "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234" }`) requestBody := []byte(`{"email": "abc@example.com", "password": "123", "clientSaltSeed": "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234" }`)
@ -76,9 +75,7 @@ func TestServerRegisterErrors(t *testing.T) {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
// Set this up to fail according to specification // Set this up to fail according to specification
testAuth := TestAuth{} server := Server{&TestAuth{}, &TestStore{Errors: tc.storeErrors}, &TestEnv{}}
testStore := TestStore{Errors: tc.storeErrors}
server := Server{&testAuth, &testStore}
// Make request // Make request
requestBody := fmt.Sprintf(`{"email": "%s", "password": "123", "clientSaltSeed": "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"}`, tc.email) requestBody := fmt.Sprintf(`{"email": "%s", "password": "123", "clientSaltSeed": "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"}`, tc.email)

View file

@ -17,7 +17,7 @@ import (
func TestServerAuthHandlerSuccess(t *testing.T) { func TestServerAuthHandlerSuccess(t *testing.T) {
testAuth := TestAuth{TestNewTokenString: auth.TokenString("seekrit")} testAuth := TestAuth{TestNewTokenString: auth.TokenString("seekrit")}
testStore := TestStore{} testStore := TestStore{}
s := Server{&testAuth, &testStore} s := Server{&testAuth, &testStore, &TestEnv{}}
requestBody := []byte(`{"deviceId": "dev-1", "email": "abc@example.com", "password": "123"}`) requestBody := []byte(`{"deviceId": "dev-1", "email": "abc@example.com", "password": "123"}`)
@ -95,7 +95,7 @@ func TestServerAuthHandlerErrors(t *testing.T) {
if tc.authFailGenToken { // TODO - TestAuth{Errors:authErrors} if tc.authFailGenToken { // TODO - TestAuth{Errors:authErrors}
testAuth.FailGenToken = true testAuth.FailGenToken = true
} }
server := Server{&testAuth, &testStore} server := Server{&testAuth, &testStore, &TestEnv{}}
// Make request // Make request
// So long as the JSON is well-formed, the content doesn't matter here since the password check will be stubbed out // So long as the JSON is well-formed, the content doesn't matter here since the password check will be stubbed out

View file

@ -66,7 +66,7 @@ func TestServerGetClientSalt(t *testing.T) {
Errors: tc.storeErrors, Errors: tc.storeErrors,
} }
s := Server{&testAuth, &testStore} s := Server{&testAuth, &testStore, &TestEnv{}}
req := httptest.NewRequest(http.MethodGet, PathClientSaltSeed, nil) req := httptest.NewRequest(http.MethodGet, PathClientSaltSeed, nil)
q := req.URL.Query() q := req.URL.Query()

View file

@ -93,7 +93,7 @@ func TestIntegrationWalletUpdates(t *testing.T) {
st, tmpFile := storeTestInit(t) st, tmpFile := storeTestInit(t)
defer storeTestCleanup(tmpFile) defer storeTestCleanup(tmpFile)
s := Init(&auth.Auth{}, &st) s := Server{&auth.Auth{}, &st, &TestEnv{}}
//////////////////// ////////////////////
t.Log("Request: Register email address - any device") t.Log("Request: Register email address - any device")
@ -259,7 +259,7 @@ func TestIntegrationChangePassword(t *testing.T) {
st, tmpFile := storeTestInit(t) st, tmpFile := storeTestInit(t)
defer storeTestCleanup(tmpFile) defer storeTestCleanup(tmpFile)
s := Init(&auth.Auth{}, &st) s := Server{&auth.Auth{}, &st, &TestEnv{}}
//////////////////// ////////////////////
t.Log("Request: Register email address") t.Log("Request: Register email address")

View file

@ -143,9 +143,8 @@ func TestServerChangePassword(t *testing.T) {
} }
for _, tc := range tt { for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
testAuth := TestAuth{}
testStore := TestStore{Errors: tc.storeErrors} testStore := TestStore{Errors: tc.storeErrors}
s := Server{&testAuth, &testStore} s := Server{&TestAuth{}, &testStore, &TestEnv{}}
// Whether we passed in wallet fields (these test cases should be passing // Whether we passed in wallet fields (these test cases should be passing
// in all of them or none of them, so we only test EncryptedWallet). This // in all of them or none of them, so we only test EncryptedWallet). This

View file

@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"lbryio/lbry-id/auth" "lbryio/lbry-id/auth"
"lbryio/lbry-id/env"
"lbryio/lbry-id/store" "lbryio/lbry-id/store"
) )
@ -33,16 +34,17 @@ const PathWrongApiVersion = "/api/"
type Server struct { type Server struct {
auth auth.AuthInterface auth auth.AuthInterface
store store.StoreInterface store store.StoreInterface
env env.EnvInterface
} }
// TODO If I capitalize the `auth` `store` and `env` fields of Store{} I can
// create Store{} structs directly from main.go.
func Init( func Init(
auth auth.AuthInterface, auth auth.AuthInterface,
store store.StoreInterface, store store.StoreInterface,
env env.EnvInterface,
) *Server { ) *Server {
return &Server{ return &Server{auth, store, env}
auth: auth,
store: store,
}
} }
type ErrorResponse struct { type ErrorResponse struct {

View file

@ -17,6 +17,14 @@ import (
// Implementing interfaces for stubbed out packages // Implementing interfaces for stubbed out packages
type TestEnv struct {
env map[string]string
}
func (e *TestEnv) Getenv(key string) string {
return e.env[key]
}
type TestAuth struct { type TestAuth struct {
TestNewTokenString auth.TokenString TestNewTokenString auth.TokenString
FailGenToken bool FailGenToken bool
@ -264,7 +272,7 @@ func TestServerHelperCheckAuth(t *testing.T) {
Errors: tc.storeErrors, Errors: tc.storeErrors,
TestAuthToken: auth.AuthToken{Token: auth.TokenString("seekrit"), Scope: tc.userScope}, TestAuthToken: auth.AuthToken{Token: auth.TokenString("seekrit"), Scope: tc.userScope},
} }
s := Server{&TestAuth{}, &testStore} s := Server{&TestAuth{}, &testStore, &TestEnv{}}
w := httptest.NewRecorder() w := httptest.NewRecorder()
authToken := s.checkAuth(w, testStore.TestAuthToken.Token, tc.requiredScope) authToken := s.checkAuth(w, testStore.TestAuthToken.Token, tc.requiredScope)

View file

@ -76,7 +76,8 @@ func TestServerGetWallet(t *testing.T) {
Errors: tc.storeErrors, Errors: tc.storeErrors,
} }
s := Server{&testAuth, &testStore} testEnv := TestEnv{}
s := Server{&testAuth, &testStore, &testEnv}
req := httptest.NewRequest(http.MethodGet, PathWallet, nil) req := httptest.NewRequest(http.MethodGet, PathWallet, nil)
q := req.URL.Query() q := req.URL.Query()
@ -234,7 +235,7 @@ func TestServerPostWallet(t *testing.T) {
Errors: tc.storeErrors, Errors: tc.storeErrors,
} }
s := Server{&testAuth, &testStore} s := Server{&testAuth, &testStore, &TestEnv{}}
requestBody := []byte( requestBody := []byte(
fmt.Sprintf(`{ fmt.Sprintf(`{