1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved. 22 */ 23 package opengrok.auth.plugin.decoders; 24 25 import opengrok.auth.plugin.entity.User; 26 import opengrok.auth.plugin.util.DummyHttpServletRequestUser; 27 import org.junit.jupiter.api.BeforeEach; 28 import org.junit.jupiter.api.Test; 29 30 import static opengrok.auth.plugin.decoders.MellonHeaderDecoder.MELLON_EMAIL_HEADER; 31 import static opengrok.auth.plugin.decoders.MellonHeaderDecoder.MELLON_USERNAME_HEADER; 32 import static org.junit.jupiter.api.Assertions.assertEquals; 33 import static org.junit.jupiter.api.Assertions.assertFalse; 34 import static org.junit.jupiter.api.Assertions.assertNotNull; 35 import static org.junit.jupiter.api.Assertions.assertNull; 36 37 public class MellonDecoderTest { 38 DummyHttpServletRequestUser dummyRequest; 39 MellonHeaderDecoder decoder = new MellonHeaderDecoder(); 40 41 @BeforeEach setUp()42 public void setUp() { 43 dummyRequest = new DummyHttpServletRequestUser(); 44 dummyRequest.setHeader(MELLON_EMAIL_HEADER, "foo@bar.cz"); 45 } 46 47 @Test testId()48 public void testId() { 49 User result = decoder.fromRequest(dummyRequest); 50 51 assertNotNull(result); 52 assertEquals("foo@bar.cz", result.getId()); 53 assertNull(result.getUsername()); 54 assertFalse(result.isTimeouted()); 55 } 56 57 @Test testMissingHeader()58 public void testMissingHeader() { 59 assertNull(decoder.fromRequest(new DummyHttpServletRequestUser())); 60 } 61 62 @Test testUsername()63 public void testUsername() { 64 dummyRequest.setHeader(MELLON_USERNAME_HEADER, "foo"); 65 User result = decoder.fromRequest(dummyRequest); 66 67 assertNotNull(result); 68 assertEquals("foo", result.getUsername()); 69 assertFalse(result.isTimeouted()); 70 } 71 } 72