|
| 1 | +// Test standalone per verificare il parsing di PARSE_SERVER_AUTH_PROVIDERS |
| 2 | +// Questo test NON avvia Parse Server e testa solo la funzione parser |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// Non importiamo helper.js per evitare di avviare il server |
| 7 | +const parsers = require('../lib/Options/parsers'); |
| 8 | +const definitions = require('../lib/Options/Definitions'); |
| 9 | + |
| 10 | +describe('Auth Providers Parser (Standalone)', () => { |
| 11 | + |
| 12 | + it('should parse auth providers JSON string to object with objectParser', () => { |
| 13 | + const authProvidersString = JSON.stringify({ |
| 14 | + facebook: { |
| 15 | + appIds: ['test-facebook-app-id-1', 'test-facebook-app-id-2'] |
| 16 | + }, |
| 17 | + google: { |
| 18 | + clientId: 'test-google-client-id', |
| 19 | + clientSecret: 'test-google-client-secret' |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + const result = parsers.objectParser(authProvidersString); |
| 24 | + |
| 25 | + expect(typeof result).toBe('object'); |
| 26 | + expect(result).not.toBeNull(); |
| 27 | + expect(result.facebook).toBeDefined(); |
| 28 | + expect(result.facebook.appIds).toEqual(['test-facebook-app-id-1', 'test-facebook-app-id-2']); |
| 29 | + expect(result.google).toBeDefined(); |
| 30 | + expect(result.google.clientId).toBe('test-google-client-id'); |
| 31 | + expect(result.google.clientSecret).toBe('test-google-client-secret'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should accept auth providers as object directly with objectParser', () => { |
| 35 | + const authProvidersObject = { |
| 36 | + facebook: { |
| 37 | + appIds: ['direct-facebook-app-id'] |
| 38 | + }, |
| 39 | + twitter: { |
| 40 | + consumerKey: 'test-consumer-key', |
| 41 | + consumerSecret: 'test-consumer-secret' |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + const result = parsers.objectParser(authProvidersObject); |
| 46 | + |
| 47 | + expect(typeof result).toBe('object'); |
| 48 | + expect(result.facebook).toBeDefined(); |
| 49 | + expect(result.facebook.appIds).toEqual(['direct-facebook-app-id']); |
| 50 | + expect(result.twitter).toBeDefined(); |
| 51 | + expect(result.twitter.consumerKey).toBe('test-consumer-key'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should throw error for invalid JSON string', () => { |
| 55 | + expect(() => { |
| 56 | + parsers.objectParser('invalid-json-string-not-json'); |
| 57 | + }).toThrow(); |
| 58 | + |
| 59 | + expect(() => { |
| 60 | + parsers.objectParser('just a plain string'); |
| 61 | + }).toThrow(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should verify auth definition in ParseServerOptions', () => { |
| 65 | + const authDefinition = definitions.ParseServerOptions.auth; |
| 66 | + |
| 67 | + expect(authDefinition).toBeDefined(); |
| 68 | + expect(authDefinition.env).toBe('PARSE_SERVER_AUTH_PROVIDERS'); |
| 69 | + expect(authDefinition.help).toContain('authentication providers'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('CRITICAL TEST: auth definition should have objectParser action', () => { |
| 73 | + const authDefinition = definitions.ParseServerOptions.auth; |
| 74 | + |
| 75 | + // Questo è il test più importante: verifica che ci sia l'action parser |
| 76 | + if (authDefinition.action) { |
| 77 | + // Se c'è l'action, testiamo che funzioni correttamente |
| 78 | + const testAuthString = JSON.stringify({ |
| 79 | + facebook: { appIds: ['test'] }, |
| 80 | + google: { clientId: 'test-client' } |
| 81 | + }); |
| 82 | + const result = authDefinition.action(testAuthString); |
| 83 | + |
| 84 | + expect(typeof result).toBe('object'); |
| 85 | + expect(result.facebook).toBeDefined(); |
| 86 | + expect(result.google).toBeDefined(); |
| 87 | + |
| 88 | + // Testa anche che accetti oggetti direttamente |
| 89 | + const testAuthObject = { facebook: { appIds: ['test2'] } }; |
| 90 | + const result2 = authDefinition.action(testAuthObject); |
| 91 | + expect(result2.facebook.appIds).toEqual(['test2']); |
| 92 | + |
| 93 | + } else { |
| 94 | + // Se non c'è l'action, questo test fallisce intenzionalmente |
| 95 | + // per evidenziare il problema |
| 96 | + fail('PROBLEMA TROVATO: auth definition manca di action parser! ' + |
| 97 | + 'Quando PARSE_SERVER_AUTH_PROVIDERS viene passata come variabile d\'ambiente, ' + |
| 98 | + 'verrà trattata come stringa invece che come oggetto. ' + |
| 99 | + 'Aggiungi "action: parsers.objectParser" alla definizione di auth in Definitions.js'); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + it('should demonstrate the problem: environment variable stays as string without parser', () => { |
| 104 | + const authDefinition = definitions.ParseServerOptions.auth; |
| 105 | + |
| 106 | + // Simula quello che succede quando viene passata una variabile d'ambiente |
| 107 | + const envValue = '{"facebook":{"appIds":["test"]}}'; |
| 108 | + |
| 109 | + if (!authDefinition.action) { |
| 110 | + // Senza action parser, la stringa rimane stringa |
| 111 | + expect(typeof envValue).toBe('string'); |
| 112 | + console.log('⚠️ PROBLEMA: La variabile d\'ambiente PARSE_SERVER_AUTH_PROVIDERS ' + |
| 113 | + 'viene passata come stringa e non viene parsata in oggetto!'); |
| 114 | + } else { |
| 115 | + // Con action parser, viene convertita in oggetto |
| 116 | + const parsed = authDefinition.action(envValue); |
| 117 | + expect(typeof parsed).toBe('object'); |
| 118 | + console.log('✓ OK: La variabile d\'ambiente viene correttamente parsata in oggetto'); |
| 119 | + } |
| 120 | + }); |
| 121 | +}); |
0 commit comments