github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/client/controllers/server_test.go (about)

     1  package controllers
     2  
     3  // besoin du server sur localhost:1323
     4  
     5  import (
     6  	"fmt"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/benoitkugler/goACVE/server/core/apiserver"
    11  	"github.com/benoitkugler/goACVE/server/core/rawdata/matching"
    12  
    13  	dm "github.com/benoitkugler/goACVE/server/core/datamodel"
    14  	rd "github.com/benoitkugler/goACVE/server/core/rawdata"
    15  
    16  	"github.com/benoitkugler/goACVE/server/core/utils/mails"
    17  )
    18  
    19  func newServerTest() ServerURL {
    20  	return ServerURL{actif: true, local: true}
    21  }
    22  
    23  func init() {
    24  	mails.InitTemplates("ressources/")
    25  }
    26  
    27  func newController(t *testing.T) *MainController {
    28  	base := dm.GetBase(true)
    29  
    30  	// Attention aux envois de mails : besoin d'un mode dev.
    31  	Server = newServerTest()
    32  
    33  	main := MainController{Base: base}
    34  	main.InitControllers(rd.Modules{})
    35  	main.ShowError = func(err error) {
    36  		t.Fatalf("error during execution : %v", err)
    37  	}
    38  	main.ShowStandard = func(msg string, wait bool) { fmt.Println(msg, wait) }
    39  	main.Background = SequentialBackground{OnError: main.ShowError}
    40  
    41  	setupOnglets(&main.Controllers)
    42  	return &main
    43  }
    44  
    45  func (ongletAides) ConfirmeSupprimeStructureaide(string) bool {
    46  	return true
    47  }
    48  
    49  func TestAides(t *testing.T) {
    50  	mainC := newController(t)
    51  	c := mainC.Controllers.Aides
    52  	nbS := len(c.Base.Structureaides)
    53  	c.CreeStructureaide(rd.Structureaide{
    54  		Nom:     "Test",
    55  		Adresse: "Rue de para dis",
    56  	})
    57  	if len(c.Base.Structureaides) != nbS+1 {
    58  		t.FailNow()
    59  	}
    60  	sa := c.Base.RechercheRapideStructureaides("Test")[0]
    61  	idP := c.Base.RechercheRapideParticipants("ben")[2].Id.Int64()
    62  	c.CreeAide(rd.Aide{
    63  		IdStructureaide: sa.Id.Int64(),
    64  		Valide:          true,
    65  		ParJour:         true,
    66  		IdParticipant:   idP,
    67  		Valeur:          25,
    68  	})
    69  	fmt.Println(c.GetStats())
    70  	rSa := c.Base.NewStructureaide(sa.Id.Int64()).RawData()
    71  	rSa.Immatriculation = "Une très belle immatriculation"
    72  	c.UpdateStructureaide(rSa)
    73  
    74  	rA := c.Base.NewParticipant(idP).GetAides(nil)[0].RawData()
    75  	rA.NbJoursMax = 5
    76  	c.UpdateAide(rA)
    77  	c.SupprimeAide(rA.Id)
    78  	c.SupprimeStructureaide(sa.Id.Int64())
    79  	fmt.Println(c.GetStats())
    80  }
    81  
    82  func (ongletCamps) ConfirmeNotifPlaceLiberee(string) (bool, bool) {
    83  	return true, false
    84  }
    85  
    86  func (ongletCamps) ConfirmeSupprimeParticipant(string) bool {
    87  	return true
    88  }
    89  
    90  func (ongletCamps) ConfirmeAjoutParticipant(dm.AccesCamp, rd.Personne, []string) bool {
    91  	return true
    92  }
    93  
    94  func TestCamps(t *testing.T) {
    95  	mainC := newController(t)
    96  
    97  	c := mainC.Controllers.Camps
    98  
    99  	pers := c.Base.RechercheRapidePersonnes(false, "*")[0].Id.Int64()
   100  	var camps []dm.AccesCamp
   101  	for _, camp := range c.Base.Camps {
   102  		if !camp.InscriptionSimple {
   103  			camps = append(camps, c.Base.NewCamp(camp.Id))
   104  		}
   105  	}
   106  	if len(camps) < 2 {
   107  		t.Fatalf("aucun camp complet")
   108  		return
   109  	}
   110  	camp, newCamp := camps[0], camps[1]
   111  	insc := len(camp.GetInscrits(nil))
   112  	c.CreeParticipant(rd.Participant{
   113  		IdPersonne:   pers,
   114  		IdCamp:       camp.Id,
   115  		ListeAttente: rd.ListeAttente{Statut: rd.Inscrit},
   116  	})
   117  	if len(camp.GetInscrits(nil)) != insc+1 {
   118  		t.FailNow()
   119  	}
   120  	part := camp.GetInscrits(nil)[0]
   121  
   122  	rc := camp.RawData()
   123  	rc.Lieu = "Montélimar"
   124  	c.UpdateCamp(rc)
   125  	if c.Base.Camps[rc.Id].Lieu != "Montélimar" {
   126  		t.FailNow()
   127  	}
   128  
   129  	rp := part.RawData()
   130  	rp.ListeAttente.Raison = "Maman !"
   131  	rp.ListeAttente.Statut = rd.Attente
   132  
   133  	c.UpdateParticipant(rp)
   134  
   135  	if isInscrit := part.RawData().ListeAttente.IsInscrit(); isInscrit {
   136  		t.FailNow()
   137  	}
   138  
   139  	inscNewCamp := len(newCamp.GetInscrits(nil))
   140  	c.Etat.IdParticipant = rd.IdParticipant(part.Id)
   141  	c.Etat.CampCurrent = rd.Id(part.GetCamp().Id)
   142  	c.ChangeCamp(newCamp.Id)
   143  	if part.GetCamp().Id != newCamp.Id {
   144  		t.FailNow()
   145  	}
   146  	if c.Etat.IdParticipant != nil {
   147  		t.Fatal("le participant devrait avoir changer de camp")
   148  	}
   149  	c.Etat.IdParticipant = rd.IdParticipant(part.Id)
   150  	c.MoveToInscrits(c.Etat.IdParticipant)
   151  
   152  	c.SupprimeParticipant(c.Base.NewParticipant(part.Id), true)
   153  	if len(newCamp.GetInscrits(nil)) != inscNewCamp {
   154  		t.FailNow()
   155  	}
   156  }
   157  
   158  func TestDons(t *testing.T) {
   159  	mainC := newController(t)
   160  	c := mainC.Controllers.Dons
   161  
   162  	pers := c.Base.RechercheRapidePersonnes(false, "*")[0]
   163  	nbDons := len(c.Base.Dons)
   164  	params := apiserver.CreateDonIn{
   165  		Don:      rd.Don{Valeur: 45, ModePaiement: "vir", DateReception: rd.Date(time.Now())},
   166  		Donateur: rd.DonDonateur{IdPersonne: rd.NewOptionnalId(pers.Id.Int64())},
   167  	}
   168  	c.CreeDon(params)
   169  	if len(c.Base.Dons) != nbDons+1 {
   170  		t.FailNow()
   171  	}
   172  	acD := c.Base.NewDon(c.Liste[0].Id.Int64())
   173  	d := acD.RawData()
   174  	d.Valeur = 58
   175  	params.Don = d
   176  	params.Donateur = c.Base.DonDonateurs[acD.Id]
   177  	c.UpdateDon(params)
   178  
   179  	// c.EditeRecusFiscaux(dm.OptionsRecuFiscal{
   180  	// 	Annee:  2020,
   181  	// 	ReEmet: true,
   182  	// })
   183  
   184  	c.Etat.DonCurrent = rd.Id(acD.Id)
   185  	c.SupprimeDon()
   186  }
   187  
   188  func (ongletInscriptions) ConfirmeSupprimeDossier(facture dm.AccesFacture) bool {
   189  	return true
   190  }
   191  func (ongletInscriptions) ConfirmeValideMailInvalide(msg string) bool {
   192  	return true
   193  }
   194  func (ongletInscriptions) DemandeEnvoiAccuseReception(_ rd.Personne) ChoixEnvoi {
   195  	return Envoi
   196  }
   197  
   198  func TestInscriptions(t *testing.T) {
   199  	mainC := newController(t)
   200  
   201  	c := mainC.Controllers.Inscriptions
   202  
   203  	c.Reset()
   204  	nbInsc := len(c.Liste)
   205  	if nbInsc == 0 {
   206  		t.Fatal("aucune inscription")
   207  	}
   208  	insc := c.Liste[0].Id
   209  	c.Etat.InscriptionCurrent = insc
   210  	c.Identifie(matching.Rattache{IdTarget: 1668, Modifications: c.Base.Personnes[1668].BasePersonne})
   211  
   212  	//c.Valide()
   213  
   214  	c.SupprimeInscription()
   215  	if len(c.Liste) != nbInsc-1 {
   216  		t.FailNow()
   217  	}
   218  }
   219  
   220  func (ongletPaiements) ConfirmeExportPaiements([]rd.Paiement, int) (keep bool, simpleHeader bool, marque bool) {
   221  	return true, false, true
   222  }
   223  
   224  func (ongletPaiements) ConfirmeSupprimePaiement(dm.AccesPaiement) bool {
   225  	return true
   226  }
   227  
   228  func TestPaiements(t *testing.T) {
   229  	mainC := newController(t)
   230  
   231  	c := mainC.Controllers.Paiements
   232  	c.Reset()
   233  
   234  	fac := c.Base.NewFacture(c.Base.Factures.Ids()[0])
   235  	nbPai := len(fac.GetPaiements(nil, true))
   236  	c.CreePaiement(rd.Paiement{
   237  		LabelPayeur:  "BK",
   238  		IdFacture:    fac.Id,
   239  		Valeur:       45.7,
   240  		ModePaiement: "cheque",
   241  	})
   242  	if len(fac.GetPaiements(nil, true)) != nbPai+1 {
   243  		t.FailNow()
   244  	}
   245  	paie := fac.GetPaiements(nil, true)[0]
   246  	rawP := paie.RawData()
   247  	rawP.NomBanque = "CA"
   248  	c.UpdatePaiement(rawP)
   249  
   250  	_ = c.ExportPaiements([]int64{rawP.Id})
   251  
   252  	c.Etat.PaiementCurrent = rd.Id(paie.Id)
   253  	c.SupprimePaiement()
   254  	if len(fac.GetPaiements(nil, true)) != nbPai {
   255  		t.FailNow()
   256  	}
   257  }
   258  
   259  func (ongletPersonnes) ConfirmeSupprimePersonne(dm.AccesPersonne) bool {
   260  	return true
   261  }
   262  func (ongletPersonnes) ConfirmeSupprimeOrganisme(dm.AccesOrganisme) bool {
   263  	return true
   264  }
   265  
   266  func TestPersonnes(t *testing.T) {
   267  	mainC := newController(t)
   268  
   269  	c := mainC.Controllers.Personnes
   270  	nbPers := len(c.Base.Personnes)
   271  	c.CreePersonne(rd.Personne{
   272  		BasePersonne: rd.BasePersonne{
   273  			Nom:  "DKSJKD",
   274  			Mail: "xxx@free;fr",
   275  		},
   276  		Cotisation: rd.Cotisation{2018, 2019},
   277  	})
   278  	newPers := c.Etat.ItemCurrent
   279  
   280  	if len(c.Base.Personnes) != nbPers+1 {
   281  		t.FailNow()
   282  	}
   283  
   284  	pers := c.Base.NewPersonne(c.Base.RechercheRapidePersonnes(false, "*")[0].Id.Int64())
   285  	rawP := pers.RawData()
   286  	fmt.Println(rawP.Cotisation.Map())
   287  	rawP.Cotisation = append(rawP.Cotisation, 2020)
   288  	c.UpdatePersonne(rawP)
   289  	fmt.Println(pers.RawData().Cotisation.Map())
   290  
   291  	c.Etat.ItemCurrent = newPers
   292  	c.SupprimeItem()
   293  
   294  	if len(c.Base.Personnes) != nbPers {
   295  		t.FailNow()
   296  	}
   297  }
   298  
   299  func TestOrganismes(t *testing.T) {
   300  	mainC := newController(t)
   301  
   302  	c := mainC.Controllers.Personnes
   303  	c.CreeOrganisme(rd.Organisme{
   304  		Nom:           "Test",
   305  		ContactPropre: true,
   306  		Contact:       rd.Coordonnees{Tels: rd.Tels{"4567894"}, Pays: "France"},
   307  		Exemplaires:   rd.Exemplaires{PubHiver: 5},
   308  		IdContactDon:  rd.NewOptionnalId(c.Base.Personnes.Ids()[0]),
   309  	})
   310  
   311  	id, ok := c.Etat.ItemCurrent.(rd.IdOrganisme)
   312  	if !ok {
   313  		t.Errorf("expected id Organisme, got %v", id)
   314  	}
   315  	raw := c.Base.NewOrganisme(id.Int64()).RawData()
   316  	raw.ContactPropre = false
   317  	raw.IdContact = rd.NewOptionnalId(c.Base.Personnes.Ids()[0])
   318  	c.UpdateOrganisme(raw)
   319  
   320  	c.supprimeOrganisme(id)
   321  }
   322  
   323  func (ongletSuiviCamps) ConfirmeAjoutDirecteurJoomeo(msg string) rd.OptionnalBool { // 0 : annule, Non : Confirme,  Oui: Confirme + mail
   324  	return 0
   325  }
   326  func (ongletSuiviCamps) ConfirmeSetupAlbumJoomeo(alreadyLinked []string) bool {
   327  	return false
   328  }
   329  func (ongletSuiviCamps) ConfirmeSupprimeCamp(camp dm.AccesCamp) bool {
   330  	return true
   331  }
   332  func (ongletSuiviCamps) ProposePasswordDirecteur(camp dm.AccesCamp, password string) bool {
   333  	return true
   334  }
   335  
   336  func TestSuiviCamps(t *testing.T) {
   337  	mainC := newController(t)
   338  
   339  	c := mainC.Controllers.SuiviCamps
   340  	nbCamps := len(c.Base.Camps)
   341  	c.CreeCamp(apiserver.CreateCampIn{
   342  		Camp: rd.Camp{
   343  			Lieu:           "Montélo",
   344  			LienCompta:     "http.goofle.fr",
   345  			SchemaPaiement: rd.SPTotal,
   346  		},
   347  	})
   348  	if len(c.Base.Camps) != nbCamps+1 {
   349  		t.FailNow()
   350  	}
   351  
   352  	camp := c.Base.NewCamp(c.Base.RechercheRapideCamps("*")[0].Id.Int64())
   353  	rawC := camp.RawData()
   354  	c.UpdateCamp(rawC)
   355  	fmt.Println(camp.RawData().Description())
   356  
   357  	c.AjouteDirecteur(camp, 1668)
   358  	if dir, has := camp.GetDirecteur(); !has || dir.Id != 1668 {
   359  		t.FailNow()
   360  	}
   361  }
   362  
   363  //
   364  //func TestSuiviDossiers(t *testing.T) {
   365  //	c := mainC.Controllers.SuiviDossiers
   366  //	camp := dm.c.Base.NewCamp(31)
   367  //	nbFac := len(c.Base.Factures)
   368  //	c.CreeFacture(1668, []rd.Item{camp.GetEquipe()[0], camp.GetEquipe()[1]})
   369  //	if len(c.Base.Factures) != nbFac+1 {
   370  //		t.FailNow()
   371  //	}
   372  //	fac := c.Base.RechercheRapideFactures("ben kug")[0].(dm.AccesFacture)
   373  //
   374  //	rawFac := fac.RawData()
   375  //	rawFac.Attestations.Presence = true
   376  //	rawFac.Info = "TEst"
   377  //	c.UpdateFacture(rawFac)
   378  //
   379  //	c.Etat.FactureCurrent = &fac
   380  //
   381  //	nbParts := len(c.Base.Participants)
   382  //	part1, part2 := fac.GetDossiers()[0], fac.GetDossiers()[1]
   383  //	c.RetireParticipant(part1, true)
   384  //	c.RetireParticipant(part2, false)
   385  //
   386  //	if len(c.Base.Participants) != nbParts-1 {
   387  //		t.FailNow()
   388  //	}
   389  //
   390  //	c.SupprimeFacture()
   391  //	if len(c.Base.Factures) != nbFac {
   392  //		t.FailNow()
   393  //	}
   394  //}
   395  //
   396  //func TestMails(t *testing.T) {
   397  //	c := mainC.Controllers.SuiviDossiers
   398  //
   399  //	ti := time.Now()
   400  //	fac := c.Base.RechercheRapideFactures("chama")[0].(dm.AccesFacture)
   401  //	part := fac.GetDossiers()[0]
   402  //	camp := part.GetCamp()
   403  //	c.EnvoiAccuseReception(fac)
   404  //	if ti.After(fac.RawData().Etat.AccuseReception.Time()) {
   405  //		t.FailNow()
   406  //	}
   407  //
   408  //	c.EnvoiPlaceLiberee(fac, part.Id, "_attente_reponse")
   409  //
   410  //	c.EnvoiPlaceLiberee(fac, part.Id, rd.RoleInscrit)
   411  //
   412  //	c.EnvoiFacture(fac, false)
   413  //	c.EnvoiFacture(fac, true)
   414  //	c.Etat.CritereCamp = rd.NewOptionnalId(camp.Id)
   415  //	c.Reset()
   416  //	fac2 := c.Liste[0]
   417  //	onError := func(index int, err string) { fmt.Println("Error ! ", index, err) }
   418  //	onSuccess := func(index int, out rd.Facture) { fmt.Println("OK", index, out) }
   419  //	c.EnvoiDocuments(camp.Id, []int64{fac.Id, fac2.GetId()}, onError, onSuccess)
   420  //
   421  //	rf := fac.RawData()
   422  //	rf.Attestations.Facture = true
   423  //	rf.Attestations.Presence = true
   424  //	c.Base.Factures[rf.Id] = rf
   425  //	c.EnvoiAttestations(fac)
   426  //	if ti.After(fac.RawData().Etat.AttestationPresence.Time()) ||
   427  //		ti.After(fac.RawData().Etat.FactureAcquittee.Time()) {
   428  //		t.FailNow()
   429  //	}
   430  //
   431  //}
   432  
   433  func TestLoadDB(t *testing.T) {
   434  	mainC := newController(t)
   435  
   436  	mainC.LoadBaseFromServer(func(i uint8) { fmt.Println(i) })
   437  }