Skip to content

Commit

Permalink
undo config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MariemBaccari committed Feb 13, 2024
1 parent cc54846 commit 9cf9443
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions be1-go/popcha/server_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package popcha

import (
"bufio"
"bytes"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"os"
"popstellar"
"popstellar/crypto"
"popstellar/hub"
Expand Down Expand Up @@ -262,9 +264,13 @@ func TestAuthorizationServerWebsocket(t *testing.T) {
// It tries to connect on /response endpoint without any additional path, and then
// tests the protocol with well-behaved clients using a valid path.
func TestAuthorizationServerWorkflow(t *testing.T) {
logTester := zltest.New(t)
logFile, err := os.CreateTemp("", "popcha_test_logs")
defer func() {
logFile.Close()
os.Remove(logFile.Name())
}()

l := zerolog.New(logTester).With().Timestamp().Logger()
l := zerolog.New(logFile).With().Timestamp().Logger()
s, err := NewAuthServer(fakeHub{}, "localhost", 3005, l)
require.NoError(t, err)
s.Start()
Expand All @@ -277,9 +283,15 @@ func TestAuthorizationServerWorkflow(t *testing.T) {

// send any message to the websocket server
err = emptyPathClient.conn.WriteMessage(websocket.TextMessage, []byte("test"))
logTester.LastEntry().ExpMsg("Error while receiving a request on /response: empty path")
require.NoError(t, err)

// Read log file contents and check for the expected log message
err = logFile.Sync()
require.NoError(t, err)
lastLine, err := getLastLine(logFile.Name())
require.NoError(t, err)
require.Contains(t, lastLine, "Error while receiving a request on /response: empty path")

// create two clients, a sender and a receiver, on a valid path
validPath := strings.Join([]string{responseEndpoint, "laoid", "authentication", "clientid", "nonce"}, "/")

Expand Down Expand Up @@ -313,7 +325,12 @@ func TestAuthorizationServerWorkflow(t *testing.T) {

<-received

logTester.LastEntry().ExpLevel(zerolog.InfoLevel)
// Read log file contents and check for the expected log message
err = logFile.Sync()
require.NoError(t, err)
lastLine, err = getLastLine(logFile.Name())
require.NoError(t, err)
require.Contains(t, lastLine, "Received the correct message from the sender client.")

require.NoError(t, clientReceiver.conn.Close())
require.NoError(t, clientSender.conn.Close())
Expand Down Expand Up @@ -486,3 +503,25 @@ func (f *fakeResponseWriter) Write(_ []byte) (int, error) {
}

func (f *fakeResponseWriter) WriteHeader(_ int) {}

// getLastLine returns the last line of a file
func getLastLine(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()

var lastLine string
scanner := bufio.NewScanner(file)
i := 1
for scanner.Scan() {
lastLine = scanner.Text()
log.Info().Msgf("line %d: %s", i, lastLine)
i++
}
if err = scanner.Err(); err != nil {
return "", err
}
return lastLine, scanner.Err()
}

0 comments on commit 9cf9443

Please sign in to comment.