Skip to content

Commit

Permalink
chore: merge develop into branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorien Grönwald committed Jan 3, 2024
2 parents f9e58db + fcb13fc commit 24fbb36
Show file tree
Hide file tree
Showing 23 changed files with 261 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Price Whisper
![Coverage](https://img.shields.io/badge/Coverage-82.5%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-82.6%25-brightgreen)
![GitHub release (by tag)](https://img.shields.io/github/v/tag/onyxmoon/hsfl-master-ai-cloud-engineering.svg?sort=semver&label=Version&color=4ccc93d)
[![Run tests (http proxy service)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-http-proxy-service.yml/badge.svg)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-http-proxy-service.yml)
[![Run tests (product-service)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-product-service.yml/badge.svg)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-product-service.yml)
Expand Down
10 changes: 8 additions & 2 deletions src/http-proxy-service/config/proxyConfig.docker.native.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
proxy:
listenAddress: 0.0.0.0:8080
proxyRoutes:
- name: User Service
- name: User Service (User Info)
context: /api/v1/user
target: http://users:3001/api/v1/user
target: http://localhost:3001/api/v1/user
- name: User Service (Login)
context: /api/v1/authentication/login
target: http://localhost:3001/api/v1/authentication/login
- name: User Service (Registration)
context: /api/v1/authentication/register
target: http://localhost:3001/api/v1/authentication/register
- name: Shoppinglist Service (Lists)
context: /api/v1/shoppinglist
target: http://shoppinglists:3002/api/v1/shoppinglist
Expand Down
10 changes: 8 additions & 2 deletions src/http-proxy-service/config/proxyConfig.docker.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
proxy:
listenAddress: 0.0.0.0:8080
proxyRoutes:
- name: User Service
- name: User Service (User Info)
context: /api/v1/user
target: http://users:3001/api/v1/user
target: http://localhost:3001/api/v1/user
- name: User Service (Login)
context: /api/v1/authentication/login
target: http://localhost:3001/api/v1/authentication/login
- name: User Service (Registration)
context: /api/v1/authentication/register
target: http://localhost:3001/api/v1/authentication/register
- name: Shoppinglist Service (Lists)
context: /api/v1/shoppinglist
target: http://shoppinglists:3002/api/v1/shoppinglist
Expand Down
8 changes: 7 additions & 1 deletion src/http-proxy-service/config/proxyConfig.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
proxy:
listenAddress: 0.0.0.0:8080
proxyRoutes:
- name: User Service
- name: User Service (User Info)
context: /api/v1/user
target: http://localhost:3001/api/v1/user
- name: User Service (Login)
context: /api/v1/authentication/login
target: http://localhost:3001/api/v1/authentication/login
- name: User Service (Registration)
context: /api/v1/authentication/register
target: http://localhost:3001/api/v1/authentication/register
- name: Shoppinglist Service (Lists)
context: /api/v1/shoppinglist
target: http://localhost:3002/api/v1/shoppinglist
Expand Down
15 changes: 13 additions & 2 deletions src/user-service/api/handler/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ func (handler *LoginHandler) Login(w http.ResponseWriter, r *http.Request) {
}

if foundUser == nil {
w.Header().Add("WWW-Authenticate", "Basic realm=Restricted")
w.WriteHeader(http.StatusUnauthorized)
return
}

if ok := handler.hasher.Validate([]byte(request.Password), foundUser.Password); !ok {
w.Header().Add("WWW-Authenticate", "Basic realm=Restricted")
w.WriteHeader(http.StatusUnauthorized)
return
}
Expand All @@ -78,6 +76,19 @@ func (handler *LoginHandler) Login(w http.ResponseWriter, r *http.Request) {
"exp": time.Now().Add(expiration).Unix(),
})

// Set the access token as a cookie
cookie := &http.Cookie{
Name: "access_token",
Value: accessToken,
Path: "/",
MaxAge: int(expiration.Seconds()),
Secure: false, // Set to true if served over HTTPS
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
}

http.SetCookie(w, cookie)

json.NewEncoder(w).Encode(loginResponse{
AccessToken: accessToken,
TokenType: "Bearer",
Expand Down
4 changes: 2 additions & 2 deletions src/user-service/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func New(
) *Router {
r := router.New()

r.POST("/api/v1/user/login", loginHandler.Login)
r.POST("/api/v1/user/register", registerHandler.Register)
r.POST("/api/v1/authentication/login/", loginHandler.Login)
r.POST("/api/v1/authentication/register/", registerHandler.Register)

r.GET("/api/v1/user/", userHandler.GetUsers)
r.GET("/api/v1/user/role/:userRole", userHandler.GetUsersByRole)
Expand Down
43 changes: 22 additions & 21 deletions src/user-service/api/router/router_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package router

import (
"bytes"
"github.com/stretchr/testify/assert"
"hsfl.de/group6/hsfl-master-ai-cloud-engineering/user-service/api/handler"
"hsfl.de/group6/hsfl-master-ai-cloud-engineering/user-service/auth"
Expand Down Expand Up @@ -33,32 +34,29 @@ func TestRouter(t *testing.T) {
assert.Equal(t, http.StatusNotFound, w.Code)
})

t.Run("/api/v1/user/login", func(t *testing.T) {
t.Run("/api/v1/authentication/login/", func(t *testing.T) {
t.Run("should return 404 NOT FOUND if method is not POST", func(t *testing.T) {
tests := []string{"HEAD", "CONNECT", "OPTIONS", "TRACE", "PATCH", "GET", "DELETE", "PUT"}

for _, test := range tests {
// given
w := httptest.NewRecorder()
r := httptest.NewRequest(test, "/api/v1/user/login", nil)
r := httptest.NewRequest(test, "/api/v1/authentication/login/", nil)

// when
router.ServeHTTP(w, r)

// then
if test == "GET" || test == "PUT" || test == "DELETE" {
assert.Equal(t, http.StatusBadRequest, w.Code)
} else {
assert.Equal(t, http.StatusNotFound, w.Code)
}
assert.Equal(t, http.StatusNotFound, w.Code)
}
})

t.Run("should call POST handler", func(t *testing.T) {
// given
w := httptest.NewRecorder()
jsonRequest := `{"email": "ada.lovelace@gmail.com", "password": "123456"}`
r := httptest.NewRequest("POST", "/api/v1/user/login", strings.NewReader(jsonRequest))
jsonRequest := `{"email": "ada.lovelace@gmail.com", "password": "12345"}`
r := httptest.NewRequest("POST", "/api/v1/authentication/login/", bytes.NewBufferString(jsonRequest))
r.Header.Set("Content-Type", "application/json")

// when
router.ServeHTTP(w, r)
Expand All @@ -68,31 +66,29 @@ func TestRouter(t *testing.T) {
})
})

t.Run("/api/v1/user/register", func(t *testing.T) {
t.Run("/api/v1/authentication/register/", func(t *testing.T) {
t.Run("should return 404 NOT FOUND if method is not POST", func(t *testing.T) {
tests := []string{"HEAD", "CONNECT", "OPTIONS", "TRACE", "PATCH", "GET", "DELETE", "PUT"}

for _, test := range tests {
// given
w := httptest.NewRecorder()
r := httptest.NewRequest(test, "/api/v1/user/register", nil)
r := httptest.NewRequest(test, "/api/v1/authentication/register/", nil)

// when
router.ServeHTTP(w, r)

// then
if test == "GET" || test == "PUT" || test == "DELETE" {
assert.Equal(t, http.StatusBadRequest, w.Code)
} else {
assert.Equal(t, http.StatusNotFound, w.Code)
}
assert.Equal(t, http.StatusNotFound, w.Code)
}
})

t.Run("should call POST handler", func(t *testing.T) {
// given
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/api/v1/user/register", strings.NewReader(`{"email": "grace.hopper@gmail.com", "password": "123456", "name": "Grace Hopper", "role": 0}`))
jsonRequest := `{"email": "grace.hopper@gmail.com", "password": "12345", "name": "Grace Hopper", "role": 0}`
r := httptest.NewRequest("POST", "/api/v1/authentication/register/", strings.NewReader(jsonRequest))
r.Header.Set("Content-Type", "application/json")

// when
router.ServeHTTP(w, r)
Expand All @@ -114,8 +110,9 @@ func TestRouter(t *testing.T) {
// when
router.ServeHTTP(w, r)

print(test)
// then
if test == "POST" || test == "PUT" || test == "DELETE" {
if test == "DELETE" || test == "PUT" {
assert.Equal(t, http.StatusBadRequest, w.Code)
} else {
assert.Equal(t, http.StatusNotFound, w.Code)
Expand Down Expand Up @@ -147,7 +144,7 @@ func TestRouter(t *testing.T) {

// when
router.ServeHTTP(w, r)

print(test)
// then
assert.Equal(t, http.StatusNotFound, w.Code)
}
Expand Down Expand Up @@ -192,7 +189,11 @@ func TestRouter(t *testing.T) {
router.ServeHTTP(w, r)

// then
assert.Equal(t, http.StatusNotFound, w.Code)
if test == "GET" || test == "DELETE" || test == "PUT" {
assert.Equal(t, http.StatusOK, w.Code)
} else {
assert.Equal(t, http.StatusNotFound, w.Code)
}
}
})

Expand Down Expand Up @@ -273,7 +274,7 @@ func setupUserRepository() user.Repository {

func setupDemoUserSlice() []*model.User {
bcryptHasher := crypto.NewBcryptHasher()
hashedPassword, _ := bcryptHasher.Hash([]byte("123456"))
hashedPassword, _ := bcryptHasher.Hash([]byte("12345"))

return []*model.User{
{
Expand Down
4 changes: 2 additions & 2 deletions src/user-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {

func setupLoginHandler(userRepository user.Repository) *handler.LoginHandler {
var jwtToken, _ = auth.NewJwtTokenGenerator(
auth.JwtConfig{SignKey: "../../auth/test-token"})
auth.JwtConfig{SignKey: "./auth/test-token"})

return handler.NewLoginHandler(setupMockRepository(userRepository),
crypto.NewBcryptHasher(), jwtToken)
Expand All @@ -49,7 +49,7 @@ func setupMockRepository(userRepository user.Repository) user.Repository {

func setupDemoUserSlice() []*model.User {
bcryptHasher := crypto.NewBcryptHasher()
hashedPassword, _ := bcryptHasher.Hash([]byte("123456"))
hashedPassword, _ := bcryptHasher.Hash([]byte("12345"))

return []*model.User{
{
Expand Down
10 changes: 7 additions & 3 deletions src/web-service/frontend/src/lib/forms/Checkbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import Checkmark from "../../assets/svg/Checkmark.svelte";
import {createEventDispatcher} from "svelte";
export let label: string;
export let id: number;
export let label: string;
export let count: number;
export let checked: boolean | undefined;
const dispatch = createEventDispatcher();
</script>

<div class="flex items-center mr-4 relative">
<div class="flex items-center relative">
<input
on:click={() => { checked = ! checked; dispatch('updateShoppingListEntry') } }
type="checkbox"
Expand All @@ -23,7 +24,10 @@

<label
for="input-{id}"
class="text-sm font-medium cursor-pointer lg:text-base { checked ? 'line-through opacity-50' : '' }">
class="text-sm font-medium cursor-pointer flex justify-center items-center gap-x-2 lg:text-base { checked ? 'line-through opacity-50' : '' }">
{label}
<span class="border-l-[1.5px] font-normal border-l-gray-dark/50 pl-2 leading-none text-gray-dark text-sm whitespace-nowrap lg:text-base { checked ? 'hidden' : '' }">
{count} Stk.
</span>
</label>
</div>
28 changes: 28 additions & 0 deletions src/web-service/frontend/src/lib/forms/Input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script lang="ts">
export let value: string | number | null;
export let label: string | null;
export let fieldName: string;
export let type: string = 'text';
export let readonly: boolean = false;
function typeAction(node: any) {
node.type = type;
}
</script>

<div class="w-full">
<label
for="{fieldName}"
class="text-sm text-gray-dark font-medium block mb-2">
{label}: *
</label>
<input
id="{fieldName}"
name="{fieldName}"
use:typeAction
required
readonly="{readonly}"
placeholder="{label}"
bind:value={value}
class="text-sm rounded-lg border px-3 py-2 w-full text-left text-green-dark/75 flex items-center justify-between transition-all duration-300 ease-in-out hover:bg-blue-light/25 lg:px-4 lg:py-3 read-only:bg-gray-light"/>
</div>
36 changes: 0 additions & 36 deletions src/web-service/frontend/src/lib/forms/InputText.svelte

This file was deleted.

2 changes: 2 additions & 0 deletions src/web-service/frontend/src/lib/forms/SubmitButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
export let label: string = 'Speichern';
export let centered: boolean = true;
export let type: "button" | "submit" | "reset" | null | undefined = 'button';
const dispatch = createEventDispatcher();
</script>

<button
type={type}
on:click={() => dispatch('submit')}
class="bg-green-light text-white rounded-xl px-5 py-2 flex items-center justify-center gap-x-2 transition-all ease-in-out duration-300 hover:bg-green-dark disabled:bg-gray-light disabled:text-gray-dark { centered ? 'mx-auto mt-8' : '' }">
<span class="text-sm lg:text-base">{label}</span>
Expand Down
2 changes: 1 addition & 1 deletion src/web-service/frontend/src/lib/navigation/NavBar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Euro from "../../assets/svg/Euro.svelte";
</script>

<aside class="fixed bottom-0 bg-white w-full rounded-t-[2.5rem] px-5 py-4 drop-shadow-navBar sm:left-0 sm:top-0 sm:w-auto sm:rounded-none sm:rounded-r-2xl sm:px-4">
<aside class="fixed bottom-0 bg-white w-full rounded-t-[2.5rem] px-5 py-4 shadow-navBar sm:shadow sm:left-0 sm:top-0 sm:w-auto sm:rounded-none sm:rounded-r-2xl sm:px-4">
<div class="hidden pt-10 pb-8 sm:block border-b-2 border-b-gray-dark/20">
<figure class="w-12 h-12 bg-blue-dark/90 rounded-full flex items-center justify-center">
<Euro classes="text-white w-6 h-6"/>
Expand Down
4 changes: 2 additions & 2 deletions src/web-service/frontend/src/lib/products/FindProduct.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import InputText from "$lib/forms/InputText.svelte";
import Input from "$lib/forms/Input.svelte";
import FetchFeedback from "$lib/products/FetchFeedback.svelte";
import {handleErrors} from "../../assets/helper/handleErrors";
import {validateEan} from "../../assets/helper/validateEan";
Expand Down Expand Up @@ -73,7 +73,7 @@

<section class="mb-6 lg:mb-8">
{#if ! productData}
<InputText
<Input
fieldName="productEan"
label="EAN des Produktes"
type="number"
Expand Down
Loading

0 comments on commit 24fbb36

Please sign in to comment.