summaryrefslogtreecommitdiffstats
path: root/auth/auth-service/src/test/java/org/onap/aaf/auth/service/test/JU_ServiceImpl_createUserCred.java
blob: 1e4e9719f82e9eb9293a88e06cd759e43aa0ed4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * ============LICENSE_START====================================================
 * org.onap.aaf
 * ===========================================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */

package org.onap.aaf.auth.service.test;

import static org.mockito.Mockito.*;

import java.nio.ByteBuffer;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.aaf.auth.dao.CachedDAO;
import org.onap.aaf.auth.dao.cass.CredDAO;
import org.onap.aaf.auth.dao.cass.UserRoleDAO;
import org.onap.aaf.auth.env.AuthzTrans;
import org.onap.aaf.auth.layer.Result;
import org.onap.aaf.auth.org.OrganizationException;
import org.onap.aaf.cadi.Hash;
import org.onap.aaf.cadi.util.FQI;
import org.onap.aaf.misc.env.Trans;

import aaf.v2_0.CredRequest;
import junit.framework.Assert;

@RunWith(MockitoJUnitRunner.class)
public class JU_ServiceImpl_createUserCred extends JU_BaseServiceImpl  {
	@Mock 
    private Result<CredDAO.Data> rcdd;	
	
	@Before
	public void setUp() throws Exception {
	    super.setUp();
	}

    @Test
    public void validCreateNewIsOwner() throws OrganizationException {
    	CredRequest cr = credRequest1();
    	final String fqi = "bob@people.onap.org";
    	when(trans.user()).thenReturn(fqi);
    	when(org.isValidPassword(trans, cr.getId(),cr.getPassword())).thenReturn("");
    	when(org.isValidCred(trans, cr.getId())).thenReturn(true);
    	when(org.canHaveMultipleCreds(cr.getId())).thenReturn(true);
		when(org.getIdentity(trans, cr.getId())).thenReturn(orgIdentity);
		when(orgIdentity.isFound()).thenReturn(true);
		final String ns = "org.onap.sample";
	    when(question.userRoleDAO().read(trans, fqi, ns+".owner")).thenReturn(Result.ok(listOf(urData(fqi,ns,"owner",100))));
	    when(question.nsDAO().read(trans, ns)).thenReturn(Result.ok(nsData(ns)));
	    when(question.credDAO().readID(trans, cr.getId())).thenReturn(Result.ok(emptyList(CredDAO.Data.class)));
	    when(question.credDAO().create(any(AuthzTrans.class), any(CredDAO.Data.class) )).thenReturn(Result.ok(credDataFound(cr,100)));
	    when(question.credDAO().readNS(trans, ns)).thenReturn(Result.ok(listOf(credDataFound(cr,100))));
	    Result<?> result = acsi.createUserCred(trans,cr);
	    // Owner may do FIRST Creds
    	Assert.assertEquals(Result.OK,result.status);
    }

    @Test
    public void validCreateNewOnlyAdmin() throws OrganizationException {
    	CredRequest cr = credRequest1();
    	final String fqi = "bob@people.onap.org";
    	when(trans.user()).thenReturn(fqi);
    	when(org.isValidPassword(trans, cr.getId(),cr.getPassword())).thenReturn("");
    	when(org.isValidCred(trans, cr.getId())).thenReturn(true);
    	when(org.canHaveMultipleCreds(cr.getId())).thenReturn(true);
		when(org.getIdentity(trans, cr.getId())).thenReturn(orgIdentity);
		when(orgIdentity.isFound()).thenReturn(true);
		final String ns = "org.onap.sample";
	    when(question.userRoleDAO().read(trans, fqi, ns+".owner")).thenReturn(Result.ok(emptyList(UserRoleDAO.Data.class)));
	    when(question.userRoleDAO().read(trans, fqi, ns+".admin")).thenReturn(Result.ok(listOf(urData(fqi,ns,"admin",100))));
	    when(question.nsDAO().read(trans, ns)).thenReturn(Result.ok(nsData(ns)));
	    when(question.credDAO().readID(trans, cr.getId())).thenReturn(Result.ok(emptyList(CredDAO.Data.class)));
	    when(question.credDAO().create(any(AuthzTrans.class), any(CredDAO.Data.class) )).thenReturn(Result.ok(credDataFound(cr,100)));
	    when(question.credDAO().readNS(trans, ns)).thenReturn(Result.ok(listOf(credDataFound(cr,100))));
	    Result<?> result = acsi.createUserCred(trans,cr);
	    // Admins may not do FIRST Creds
    	Assert.assertEquals(Result.ERR_Denied,result.status);
    }

    @Test
    public void validCreateExisting() throws OrganizationException {
    	CredRequest cr = credRequest1();
    	when(org.isValidPassword(trans, cr.getId(),cr.getPassword())).thenReturn("");
    	when(org.isValidCred(trans, cr.getId())).thenReturn(true);
    	when(org.canHaveMultipleCreds(cr.getId())).thenReturn(true);
		when(org.getIdentity(trans, cr.getId())).thenReturn(orgIdentity);
		when(orgIdentity.isFound()).thenReturn(true);
		String ns = "org.onap.sample";
	    when(question.nsDAO().read(trans, ns)).thenReturn(Result.ok(nsData(ns)));
	    
	    CredDAO.Data cdd = credDataFound(cr,100);
	    when(question.credDAO().create(any(AuthzTrans.class), any(CredDAO.Data.class) )).thenReturn(Result.ok(cdd));
	    when(question.credDAO().readID(trans, cr.getId())).thenReturn(Result.ok(listOf(cdd)));

	    Result<?> result = acsi.createUserCred(trans,cr);
    	Assert.assertEquals(Result.OK,result.status);
    }

    private CredRequest credRequest1() {
    	CredRequest cr = new CredRequest();
    	cr.setId("m12345@sample.onap.org");
    	cr.setPassword("BobAndWeave");
    	cr.setType(CredDAO.RAW);
    	return cr;
    }
    
   private CredDAO.Data credDataFound(CredRequest cr, int days) {
    	CredDAO.Data cdd = new CredDAO.Data();
    	cdd.id = cr.getId();
    	cdd.ns = FQI.reverseDomain(cr.getId());
    	cdd.other = 12345;
    	cdd.tag = "1355434";
    	cdd.type = CredDAO.BASIC_AUTH_SHA256;
    	try {
			cdd.cred = ByteBuffer.wrap(Hash.hashSHA256(cr.getPassword().getBytes()));
		} catch (NoSuchAlgorithmException e) {
			Assert.fail(e.getMessage());
		}
    	GregorianCalendar gc = new GregorianCalendar();
    	gc.add(GregorianCalendar.DAY_OF_YEAR, days);
    	cdd.expires = gc.getTime();
    	return cdd;
    }
    
}