<commit_before>﻿module Jim.Tests.IntegrationTests

open Suave
open Suave.Types
open Suave.Http
open Suave.Web
open Suave.Testing

open System
open System.Net
open System.Net.Http
open System.Text

open Jim.ApplicationService
open Jim.Domain
open Jim.WebServer

open NodaTime

open EventPersistence

open Fuchu
open Swensen.Unquote.Assertions

let run_with' = run_with default_config

let streamId = "testStream"

let storeWithEvents events =
    let projection = fun (x: Event) -> ()
    let store = EventPersistence.InMemoryStore<Event>(projection) :> IEventStore<Event>
    if not (List.isEmpty events) then
        store.AppendToStream streamId -1 events |> Async.RunSynchronously
    store

let guid1 = new Guid("3C71C09A-2902-4682-B8AB-663432C8867B")
let epoch = new Instant(0L)

[<Tests>]
let tests =
    testList "Integration tests"
        [
        testCase "Should be able to create a user" (fun () ->
            let store = storeWithEvents []

            let postData = new ByteArrayContent(Encoding.UTF8.GetBytes("""{"name":"<NAME>", "email":"<EMAIL>","password":"<PASSWORD>"}"""))

            let actual = (run_with' (webApp <| new AppService(store, streamId))) |> req HttpMethod.POST "/users/create" (Some postData)

            test <@ actual.Contains("\"id\":") && actual.Contains("User created") @>)

        testCase "Should be able to rename a user" (fun () ->
            let store = storeWithEvents [UserCreated { Id = guid1; Name="<NAME>"; Email=EmailAddress "<EMAIL>"; PasswordHash=PasswordHash "<PASSWORD>"; CreationTime = epoch} ]

            let postData = new ByteArrayContent(Encoding.UTF8.GetBytes("""{"id":"3C71C09A-2902-4682-B8AB-663432C8867B", "name":"<NAME>"}"""))

            let actual = (run_with' (webApp <| new AppService(store, streamId))) |> req HttpMethod.PUT "/users/3C71C09A-2902-4682-B8AB-663432C8867B/name" (Some postData)

            test <@ actual.Contains("<NAME>") && actual.Contains("Name changed to") @>)
        ]<commit_msg>test needs to meet password length rule<commit_after>﻿module Jim.Tests.IntegrationTests

open Suave
open Suave.Types
open Suave.Http
open Suave.Web
open Suave.Testing

open System
open System.Net
open System.Net.Http
open System.Text

open Jim.ApplicationService
open Jim.Domain
open Jim.WebServer

open NodaTime

open EventPersistence

open Fuchu
open Swensen.Unquote.Assertions

let run_with' = run_with default_config

let streamId = "testStream"

let storeWithEvents events =
    let projection = fun (x: Event) -> ()
    let store = EventPersistence.InMemoryStore<Event>(projection) :> IEventStore<Event>
    if not (List.isEmpty events) then
        store.AppendToStream streamId -1 events |> Async.RunSynchronously
    store

let guid1 = new Guid("3C71C09A-2902-4682-B8AB-663432C8867B")
let epoch = new Instant(0L)

[<Tests>]
let tests =
    testList "Integration tests"
        [
        testCase "Should be able to create a user" (fun () ->
            let store = storeWithEvents []

            let postData = new ByteArrayContent(Encoding.UTF8.GetBytes("""{"name":"<NAME>", "email":"<EMAIL>","password":"<PASSWORD>"}"""))

            let actual = (run_with' (webApp <| new AppService(store, streamId))) |> req HttpMethod.POST "/users/create" (Some postData)

            test <@ actual.Contains("\"id\":") && actual.Contains("User created") @>)

        testCase "Should be able to rename a user" (fun () ->
            let store = storeWithEvents [UserCreated { Id = guid1; Name="<NAME>"; Email=EmailAddress "<EMAIL>"; PasswordHash=PasswordHash "<PASSWORD>"; CreationTime = epoch} ]

            let postData = new ByteArrayContent(Encoding.UTF8.GetBytes("""{"id":"3C71C09A-2902-4682-B8AB-663432C8867B", "name":"<NAME>"}"""))

            let actual = (run_with' (webApp <| new AppService(store, streamId))) |> req HttpMethod.PUT "/users/3C71C09A-2902-4682-B8AB-663432C8867B/name" (Some postData)

            test <@ actual.Contains("<NAME>") && actual.Contains("Name changed to") @>)
        ]
{'fraction_non_alphanumeric': 0.13577154308617234, 'fraction_numerical': 0.03907815631262525, 'mean_word_length': 3.7254437869822485, 'pattern_counts': {'":': 12, '<': 33, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3490077', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1261, 'n_words': 316}